In this article I will explain how to implement Remember Me CheckBox functionality i.e. Remember the UserName and Password for the user when he visits next time using Cookies in ASP.Net
 
HTML Markup
I have a simple HTML Form below which has two ASP.Net TextBox controls txtUserName and txtPassword and a CheckBox control chkRememberMe to allow user specify whether he wants the page to remember the UserName and Password when he visits next time, finally an ASP.Net Button btnLogin which when clicked will save the entered UserName and Password in the Cookie.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    UserName:
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
    Password:
    <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox><br />
    Remember me:
    <asp:CheckBox ID="chkRememberMe" runat="server" /><br />
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login_Click" />
    </form>
</body>
</html>
 
 
Saving the UserName and Password in Cookie
When the Button btnLogin is clicked the following event handler is executed which first checks whether the chkRememberMe is checked. If it is checked then it save the UserName and Password in the Cookies and sets their expiration date to 30 days in future from the current date. And if it is not checked then it sets the expiration date to 1 day in past so that Cookie is destroyed.
 
C#
protected void Login_Click(object sender, EventArgs e)
{
    if (chkRememberMe.Checked)
    {
        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
        Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
    }
    else
    {
        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
        Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
 
    }
    Response.Cookies["UserName"].Value = txtUserName.Text.Trim();
    Response.Cookies["Password"].Value = txtPassword.Text.Trim();
}
 
 
VB.Net
Protected Sub Login_Click(ByVal sender As Object, ByVal e As EventArgs)
    If chkRememberMe.Checked Then
        Response.Cookies("UserName").Expires = DateTime.Now.AddDays(30)
        Response.Cookies("Password").Expires = DateTime.Now.AddDays(30)
    Else
        Response.Cookies("UserName").Expires = DateTime.Now.AddDays(-1)
        Response.Cookies("Password").Expires = DateTime.Now.AddDays(-1)
    End If
    Response.Cookies("UserName").Value = txtUserName.Text.Trim
    Response.Cookies("Password").Value = txtPassword.Text.Trim
End Sub
 
 
Populating the UserName and Password from Cookie and setting it in TextBoxes
Now in Page_Load event we will check if the Cookie exists and if yes then we will set the TextBoxes for UserName and Password with their respective Cookie value.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
        {
            txtUserName.Text = Request.Cookies["UserName"].Value;
            txtPassword.Attributes["value"] = Request.Cookies["Password"].Value;
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        If ((Not (Request.Cookies("UserName")) Is Nothing) _
                    AndAlso (Not (Request.Cookies("Password")) Is Nothing)) Then
            txtUserName.Text = Request.Cookies("UserName").Value
            txtPassword.Attributes("value") = Request.Cookies("Password").Value
        End If
    End If
End Sub
 
 
Demo
 
Downloads