Authentication is the process of identifying users. Authorization is the process of granting access to those users based on identity. Together, authentication and authorization secures our Web application.
Authentication - Who is the User?
Authorization - What rights the user has? What resources the user can access?
There are three types of authentication in ASP.NET,
- Windows authentication
- Forms authentication
- Passport Authentication
These are the followings steps to use forms authentication.
Step 1: Open Visual Studio then go to the File Menu where we click New, then Project and select the ASP.NET web application and assign the name of the application in pop up menu.
Step 2: After selecting the web application select an empty template here.
Step 3: In our web application here we add two pages one login.aspx and another welcome.aspx.
Step 4: Now we set web.config file to implement the authentication of web application.
<configuration>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="login.aspx" defaultUrl="welcome.aspx">
<credentials passwordFormat="Clear">
<user name="Utpal" password="addi@123"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
Steps 5: Now we create a login form in login.aspx and welcome.aspx page. For that we will write the code for login form here,
Login.aspx HTML:
<div style="font-family:Arial">
<table style="border: 1px solid black">
<tr>
<td colspan="2">
<b>Login</b>
</td>
</tr>
<tr>
<td>
User Name
</td>
<td>
:<asp:TextBox ID="txtUserName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
:<asp:TextBox ID="txtPassword" TextMode="Password" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Remember me?</td>
<td>
<asp:CheckBox ID="chkboxPersist" runat="server" />
< /td>
</tr>
<tr>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Login" />
</td>
</tr>
</table>
<br />
<p>
<asp:Label ID="Msg" ForeColor="red" runat="server" />
</p>
</div>
Welcome.aspx HTML:
<h1>Welcome Page</h1>
<h1>Welcome Page</h1>
Step 6: Here we write the code for Login button in login.aspx.cs page.
protected void Login_Click(object sender, EventArgs e) {
if (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text)) {
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkboxPersist.Checked);
}
else {
Msg.Text = "Invalid User Name and/or Password";
}
}
Step 7: After writing the code of these pages now we are ready to execute the web application. To run the application we click Ctrl+F5 .
protected void Login_Click(object sender, EventArgs e) {
if (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text)) {
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkboxPersist.Checked);
}
else {
Msg.Text = "Invalid User Name and/or Password";
}
}
Step 7: After writing the code of these pages now we are ready to execute the web application. To run the application we click Ctrl+F5 .
No comments:
Post a Comment