Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to implement Remember Me option in login page in ASP.NET

Author: Alanna Kremer
Download Source Code :

We will see in this article that how we can implement remember me option in login page in ASP.NET.


You see remember me CheckBox in almost every login page of different websites. This can be implemented easily with the help of cookies. Cookies are small piece of data or information sent from a website and stored in user’s browser and later this information can be retrieved by the website to remember user. Users can disable cookies in their browsers so we have to check if cookies are enabled or not. Cookies are a good way to implement remember me option for login detail.
You can use Login control from Visual Studio Toolbox or you can use your functionality and layout for Login page. Remember me CheckBox is provided with built-in login control in Visual Studio Toolbox. I have used my own functionality and layout instead of Login control. Implementing remember me option for both techniques has no great difference. I will show remember me option with my own added CheckBox contro in Login page. I have already explained about how to create a Login page in my previous article. I am using same SQL Server database table, stored procedure and function in code file for this article as I have used for previous article.
Let’s see how we can implement Remember Me option.
  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
  4. Website > Add New Item > Web Form > Click Add (Default.aspx)
  5. Website > Add New Item > Web Form > Click Add (Rename as UserPage.aspx
  6. Add code below in Default.aspx

            <td colspan="2" style="text-align: center; height: 21px;">
                <span style="font-size: 24pt">Login</span>
            <td style="width: 71px" valign="top">
                <asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label></td>
            <td style="width: 178px; text-align: right;" valign="top">
                <asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox></td>
            <td style="width: 71px; height: 26px;">
                <asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label></td>
            <td style="width: 178px; height: 26px; text-align: right;">
                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            <td colspan="2" style="text-align: right">
                <asp:CheckBox ID="ChkRemember" runat="server" Text="Remember Me" /></td>
            <td style="width: 71px; height: 27px;">
            <td style="width: 178px; text-align: right; height: 27px;">
                 <asp:Button ID="btnLogin" runat="server" Style="position: relative"
                    Text="Login" onclick="btnLogin_Click" /></td>
    <br />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
  7. Stored procedure used to verify account

    CREATE PROCEDURE dbo.VerifyAccount
           @Email Varchar(100),
           @Password NVarchar(50),
           @Verify BIT OUTPUT
           DECLARE @RecordCount INT
           SELECT @RecordCount = Count(*) FROM Account WHERE Email = @Email AND Password = @Password
           IF @RecordCount = 1
                  SET @Verify = 1
                  SET @Verify = 0

  8. Open code behind file and include following namespace.

    using System.Data;
    using System.Data.SqlClient;

    Imports System.Data
    Imports System.Data.SqlClient
  9. Write below function in code behind file to verify user.

    public bool VerifyAccount(string email, string password)
        bool result;
        string connstring = "Data Source=Local;Initial Catalog=SampleApplicationDatabase;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connstring);
        SqlCommand cmd = new SqlCommand("VerifyAccount", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = email;
        cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = password;
        SqlParameter pVerify = new SqlParameter("@Verify", SqlDbType.Bit);
        pVerify.Direction = ParameterDirection.InputOutput;
        pVerify.Value = DBNull.Value;
            if ((bool)pVerify.Value == true)
                result = true;
                result = false;
        catch (Exception ex)
            result = false;
        return result;
    Public Function VerifyAccount(email As String, password As String) As Boolean
        Dim result As Boolean
        Dim connstring As String = "Data Source=Local;Initial Catalog=SampleApplicationDatabase;Integrated Security=True"
        Dim conn As New SqlConnection(connstring)
        Dim cmd As New SqlCommand("VerifyAccount", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = email
        cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = password
        Dim pVerify As New SqlParameter("@Verify", SqlDbType.Bit)
        pVerify.Direction = ParameterDirection.InputOutput
        pVerify.Value = DBNull.Value
            If CBool(pVerify.Value) = True Then
                result = True
                result = False
            End If
        Catch ex As Exception
            result = False
        End Try
        Return result
    End Function

  10. Write code below in Page Load and Button click events to implement Remember me option


    protected void Page_Load(object sender, EventArgs e)
        if (Request.Cookies["Remember"] != null)
            HttpCookie cookieRemember = Request.Cookies.Get("Remember");
            txtEmail.Text = cookieRemember.Values["Email"];
            txtPassword.Text = cookieRemember.Values["Password"];
            Response.Cookies["Remember"].Expires = DateTime.Now;
    protected void btnLogin_Click(object sender, EventArgs e)
        bool result = VerifyAccount(txtEmail.Text, txtPassword.Text);
        if (result == true)
            if (ChkRemember.Checked)
         HttpCookie cookieRemember = new HttpCookie("Remember");
                cookieRemember.Values.Add("Email", txtEmail.Text);
                cookieRemember.Values.Add("Password", txtPassword.Text);
                cookieRemember.Expires = DateTime.Now.AddDays(10);
            lblMessage.Text = "Sorry you cannot login, please provide correct Email and password";
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
       If Request.Cookies("Remember") IsNot Nothing Then
           Dim cookieRemember As HttpCookie = Request.Cookies.Get("Remember")
           txtEmail.Text = cookieRemember.Values("Email")
           txtPassword.Text = cookieRemember.Values("Password")
           Response.Cookies("Remember").Expires = DateTime.Now
       End If
    End Sub
    Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Dim result As Boolean = VerifyAccount(txtEmail.Text, txtPassword.Text)
        If result = True Then
            If ChkRemember.Checked Then
         Dim cookieRemember As New HttpCookie("Remember")
               cookieRemember.Values.Add("Email", txtEmail.Text)
               cookieRemember.Values.Add("Password", txtPassword.Text)
                cookieRemember.Expires = DateTime.Now.AddDays(10)
            End If
            lblMessage.Text = "Sorry you cannot login, please provide correct Email and password"
        End If
    End Sub
    In Login Button click event, I have verified user information first and then I have confirmed that the remember me CheckBox is checked. I have created HttpCookie object and added email and password in it. Then I have set 10 days as expire time of the Cookie.
    In Page Load event, first I have checked for Cookie existence in the user’s browser. If the specified cookie exists then I have retrieved email and password values.
  11. Set Default.aspx as start page and Press F5.


Add Article Comment:
Name :
Email Address :
Comments :
<< How to use DropDownList, RadioButtonList and CheckBoxList to edit GridView cells in ASP.NET

Disclaimer - Privacy
© 2002-2017