Form Authentication from Membership


Forms authentication lets you authenticate users by using your own code and then maintain an authentication token in a cookie or in the page URL.

Form authentication is cookie based, as ASP.NET places a cookie in the client machine in order to track the user. If the user requests a secure page and has not logged in, then ASP.NET redirects him/her to the login page. Once the user is authenticated, he/she will be allowed to access the requested page.

Membership


ASP.NET membership gives you a built-in way to validate and store user credentials. ASP.NET membership therefore helps you manage user authentication in your Web sites. You can use ASP.NET membership with ASP.NET forms authentication by using with the ASP.NET login controls to create a complete system for authenticating users.

● Open "Web Administration Tool"!
log1
● Create users !
log2

web.config to set Form Authentication

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    
 <system.web>
		<compilation debug="true" targetFramework="4.0"/>
   <authentication  mode="Forms">
     <forms name="myname" loginUrl="~/login.aspx" timeout="10" cookieless="UseCookies" defaultUrl="welcome.aspx">
      
      
     </forms>
  </authentication>
	</system.web>
</configuration>

● Login Page -login.aspx


log3

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <link href="bootstrap-3.3.5-dist/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
    
</head>
<body>
    <form id="form1" runat="server">
    <div style=" width:400px; margin:10% auto">
        <asp:Login ID="Login1" runat="server" onauthenticate="Login1_Authenticate" 
            TitleText="Log In : codetpoint.com" Width="400px">
                <TitleTextStyle BackColor="Green" ForeColor="Yellow"  Height="40px" Font-Size="1.25em" />
                <LabelStyle CssClass="form-control" />
                <TextBoxStyle CssClass="form-control " />
                <LoginButtonStyle CssClass="btn btn-success" />
       </asp:Login>
    </div>
    </form>
</body>
</html>

Code behind Login Page -login.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class login : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
       if (Membership.ValidateUser(Login1.UserName, Login1.Password)) 
         {
             e.Authenticated = true;
             FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
         }
         else
         {
             e.Authenticated = false;
         }

       
    }
}

● Welcome Page -welcome.aspx


log4
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="welcome.aspx.cs" Inherits="welcome" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <link href="bootstrap-3.3.5-dist/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
     <link href="bootstrap-3.3.5-dist/css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <form id="form1" runat="server">
  <table  style="width:100%">
  <tr>
  <td style="width:75%; text-align:left" >
  <div>
  <ul class="nav nav-pills">
  <li class="active"><a href="welcome.aspx">Home</a></li>
  <li ><a href="#">Services</a></li>
  <li><a href="#">Courses</a></li>
  <li ><a href="#">About</a></li>
  </ul>
  </div>
  </td>

  <td  style="width:25%; text-align:left" >
  <div>
  <asp:LoginName ID="LoginName2" runat="server" />|
  <asp:LoginStatus ID="LoginStatus2" runat="server"  onloggingout="LoginStatus1_LoggingOut" />
  </div>
  </td>
  </tr>
 </table>
  </form>
</body>
</html>

Code behind Welcome Page -welcome.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!User.Identity.IsAuthenticated)
        {
            Response.Redirect("login.aspx");
            
       }
    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();
      
    }
  }