In this article I will explain with an example, how to Remember UserName and Password using RememberMe CheckBox when user visits next time using Cookies in ASP.Net with C# and VB.Net.
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For capturing UserName and Password.
CheckBox – For capturing user input.
Button – For submitting the Form.
The Button has been assigned with an OnClick event handler.
<table>
    <tr>
        <td>UserName:</td>
        <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Remember me:</td>
        <td><asp:CheckBox ID="chkRememberMe" runat="server" /></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="OnLogin" /></td>
    </tr>
</table>
 
 

Populating the UserName and Password from Cookie and setting it in TextBoxes

Inside the Page_Load event handler, a check is performed if the Cookies are not NULL.
Then, the values of UserName and Password are set in TextBox Control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.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 ObjectByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        If Request.Cookies("UserName")IsNot Nothing And Also Request.Cookies("Password")IsNot Nothing Then
            txtUserName.Text = Request.Cookies("UserName").Value
            txtPassword.Attributes("value") = Request.Cookies("Password").Value
        End If
    End If
End Sub
 
 

Saving the UserName and Password in Cookie

When the Login Button is clicked, a check is performed if CheckBox is checked then the UserName and Password is saved in the Cookies and their expiry date to 30 days in future from the current date is set.
And if it is not checked then, it sets the expiry date to 1 day in past so that Cookies will destroyed.
C#
protected void OnLog in(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 OnLogin(ByVal sender As ObjectByVal 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
 
 

Screenshot

Remember UserName and Password using RememberMe CheckBox and Cookies in ASP.Net
 
 

Demo

 
 

Downloads