This article is focused on using forms authentication. It uses cookies to allow applications to track users throughout their visit. First of all, to enable form authentication edit your Web.Config file
Edit your <authentication> part in Web.Config as following:
<authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="Login.aspx" defaultUrl="Home.aspx">
</forms>
</authentication>
Login Page : Login.aspx
<asp:TextBox ID="UserId" runat="server"></asp:TextBox>
<br/>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<br/>
<asp:CheckBox ID="Persist" runat="server" Text="Remember Me" />
<br/>
<asp:Button ID="Login" runat="server" onclick="Login_Click" Text="Login" />
In Login.aspx.cs, after successful verification of login credential use the following code to redirect from login page
using System.Web.Security;
FormsAuthentication.RedirectFromLoginPage(Login_TextBox_UsedId.Text, Persist.Checked);
Here Persist.Checked is used in regards to Remember Me textbox.
To restrict users from accessing any page without login, use the following code in Page_Load event:
using System.Web.Security;
if (User.Identity.IsAuthenticated == false)
FormsAuthentication.RedirectToLoginPage();
For Signing out, use the follwing code in your logout page:
using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Home.aspx");
}
If you want that your previous page to expire after successfull SignOut then use the following code with FormsAuthentication.SignOut(); in logout page
using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Redirect("Home.aspx");
}
Edit your <authentication> part in Web.Config as following:
<authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="Login.aspx" defaultUrl="Home.aspx">
</forms>
</authentication>
Login Page : Login.aspx
<asp:TextBox ID="UserId" runat="server"></asp:TextBox>
<br/>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<br/>
<asp:CheckBox ID="Persist" runat="server" Text="Remember Me" />
<br/>
<asp:Button ID="Login" runat="server" onclick="Login_Click" Text="Login" />
In Login.aspx.cs, after successful verification of login credential use the following code to redirect from login page
using System.Web.Security;
FormsAuthentication.RedirectFromLoginPage(Login_TextBox_UsedId.Text, Persist.Checked);
Here Persist.Checked is used in regards to Remember Me textbox.
To restrict users from accessing any page without login, use the following code in Page_Load event:
using System.Web.Security;
if (User.Identity.IsAuthenticated == false)
FormsAuthentication.RedirectToLoginPage();
For Signing out, use the follwing code in your logout page:
using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Home.aspx");
}
If you want that your previous page to expire after successfull SignOut then use the following code with FormsAuthentication.SignOut(); in logout page
using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Redirect("Home.aspx");
}
