Pages

Aug 11, 2010

Simple login page using Forms's Authentication + Asp.Net + C#

Login Page Implementation[Forms Authentication] :
Login page implementation is a basic need in most applications.
Now lets see a simple implementation of the Login Page in asp.net.
Create a page named Login.aspx  and add a table like

Login:

<table align="center">    
      <tr>
        <td>
          UserName :</td>
        <td>&nbsp;
          <asp:TextBox ID="txtUserName" runat="server" /></td>
        <td>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
            ControlToValidate="txtUserName"
            Display="Dynamic" 
            ErrorMessage="Please Enter User Name" 
            runat="server" />
        </td>
      </tr>
      <tr>
        <td>
          Password  :</td>
        <td>
          <asp:TextBox ID="txtPassword" TextMode="Password" 
             runat="server" />
        </td>
        <td>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" 
            ControlToValidate="txtPassword"
            ErrorMessage="Please Enter Password." 
            runat="server" />
        </td>
        </tr>   
          <tr>
          <td colspan="3" align="left">
            <asp:Button ID="Submit1" OnClick="Logon_Click" Text="Log On" 
                runat="server" />
            </td>
          </tr>  
          <tr>
            <td colspan="3">
                <p>
                  <asp:Label ID="Msg" ForeColor="red" runat="server" />
                </p>
            </td>
          </tr> 
    </table>

Now add the code-behind for login page like

     // if you have more users then store the username and
     // password in back-end database and perform the check

     if ((txtUserName.Text == "mukunda") && (txtPassword.Text == "easyone1"))
        {
            // This statement will redirect to start page automatically.
            FormsAuthentication.RedirectFromLoginPage
               (txtUserName.Text, false);
        }
        else
        {
            // to show error message for invalid login.
            Msg.Text = "Invalid credentials. Please try again.";
        }

To make the above code work we need to add the following to web config

        <authentication mode="Forms">
      <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH">
  </forms>
  </authentication>
  <authorization>
<deny users="?"/>
  </authorization>
      inside <system.web> tag.

And in the Main page you can access the logged user name and
show it in a label like
lblEmpID.Text = Context.User.Identity.Name;

LogOut :
To log out the current user you need to add a link button in main page
<asp:LinkButton ID="Submit1" runat="server" 
OnClick="Signout_Click">Sign Out</asp:LinkButton>    
    
Code-Behind:

    /// <summary>
    /// event for signingout the current page & redirect to Login page
    /// </summary>    
    protected void Signout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("Login.aspx");
    }
  
Now a simple login implementation using Forms Authentication is done!

1 comment:

Anonymous said...

onloggedin="LoginUser_LoggedIn"

protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
Response.Redirect("~/about.aspx");
}