Table of contents
1.
Introduction
2.
WebForms
2.1.
Step 1
2.2.
step 2
2.3.
step 3
2.4.
Step 4
2.5.
Step 5
2.6.
Step 6
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

WebForms Authentication

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

You might have used google forms or a spreadsheet where you need to log in to access the content inside the page. It allows you to access the page only if your mail is valid and verified by the owner. This is called “Authentication”. Similarly, the authentication technique is used for the websites too.

ASP.NET WebPages, also known as WebForms, is a web framework supported by ASP.NET. The WebForms application can be written in languages C# and VB.Net. The webForm pages are server controls used to render the pages, and they are reusable. They are used for user authentication. Let’s learn what authentication in webForms is and the steps we need to follow in the article.

WebForms

To create a webForm, we have to follow the steps listed below.  

Step 1

Create a new project in the visual studio code and select the ASP.NET web application. Then assign a name for that application in the menu.

Source

step 2

Select an empty web forms Template and select No Authentication in the Authentication field.

Source

step 3

Authorize a Secure Socket Layer (SSL) for the form. SSL is a protocol that allows servers and clients to communicate securely using encryption.

Source

Step 4

Select Properties on the right-click in solution explorer, set the project URL, and save it. The final URL should be the same as the SSL URL.

Source

Step 5

After the SSL is enabled, run the application. This may ask us to install an IIS Express SSL certificate. Install it and run the application.

It will show us the following output:

Source

This application has two links, Register and Login, in the upper right corner.  

Step 6

The user can register or log in by clicking on the links and fill the details. The links will redirect the users to their respective pages.

Source

Source

Let’s learn to code the webForms now.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="FormAuth.Login" %>  
<!DOCTYPE html>  
<html>  
  <head runat="server">  
    <title>WebForms</title>  
  </head>  
<body>  
  <form id="form1" runat="server">  
     <h3> Login Page</h3>  
     <table>  
       <tr>  
          <td>Email:</td>  
          <td><asp:TextBox ID="email" runat="server" />  
          </td>  
     <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="email" Display="Dynamic" 
     ErrorMessage="Cannot be empty." runat="server" />  
          </td>  
       </tr>  
       <tr>  
          <td>Password:</td>  
          <td><asp:TextBox ID="UserPass" TextMode="Password"   
             runat="server" />  
          </td>  
          <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="UserPass"
          ErrorMessage="Cannot be empty." runat="server" />  
           </td>  
       </tr>  
       <tr>  
           <td>Remember me?</td>  
           <td><asp:CheckBox ID="chkboxPersist" runat="server" />  
           </td>  
       </tr>  
     </table>  
	<asp:Button ID="Submit1" OnClick="Login" Text="Log In"   
       runat="server" />  
   <p><asp:Label ID="Msg" ForeColor="red" runat="server" />  
   </p>  
    </form>  
  </body>  
</html>


We created a form in the above code with email and password fields. The fields are integrated with validators and a submit button.

using System;  
namespace FormAuth {  
  public partial class Login: System.Web.UI.Page {  
    protected void Login_Click(object sender, EventArgs e) {  
      if(FormsAuthentication.Authenticate(UserName.Text, UserPass.Text)) {  
              FormsAuthentication.RedirectFromLoginPage(UserName.Text, chkboxPersist.Checked);  
      } else {  
          Msg.Text = "Invalid User Name and/or Password";  
       }  
     }  
  }  
}  


The user should be prevented from accessing the website by giving the link directly in the browser. So for that, we should provide a condition for secure authentication.

After the user logs in, the website will display the home page with the user's login status.

Source

FAQs

  1. What is ASP.NET?
    ASP.NET is an open-source web framework that works on the server-side to produce dynamic web pages using HTML, CSS, JavaScript, TypeScript.
     
  2. What is Webform authentication?
    The form authentication used for internet web applications to verify and validate the ID or username of the user trying to login to the website is called Web Form Authentication.
     
  3. What is IIS authentication?
    IIS authentication is used to control the user from accessing different parts of websites or pages that are private. It includes anonymous authentication, basic authentication, client certificate mapping authentication, etc. 
     
  4. What is the difference between form-based authentication and basic authentication?
    Both the authentication verifies the users and their mails. But the form-based authentication uses cookies, unlike basic authentication.
     
  5. Is form-based authentication secure?
    The form-based authentication is not entirely secure. This authentication can expose the user names and passwords if the connection is not made over SSL.

Key Takeaways

We discussed how to create a web application and secure that webform with login and user registration in this article. We learned to configure the site to use forms authentication and created a login page. Finally, we learned to determine whether a user is authenticated or not. 

Hey Ninjas! Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Happy Learning!

Live masterclass