Implement “Remember Me” in ASP.Net login page

Mohammed Imtiyaz Sep 20, 2015

In this tutorial we will learn how to implement “Remember Me” in ASP.Net login web page.

How does it works?

The login credentials are saved to the web browser’s cookies and will remain there for a specific time unless you clear the browser’s history. The credentials will be called the next time you login the same web page.

Here we can see how the credentials are saved to browser’s cookies using C# code and Vb.net code.

C#

C# Page Load Event

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            //Check if the browser support cookies.
            if (Request.Browser.Cookies)
            {
                //Check if the cookie with name "C_LOGIN" exists on web browser of the user's machine.
                if (Request.Cookies["C_LOGIN"] != null)
                {
                    //Note: You have two options: Either you can display the login page filled with credentials in it.
                    //or you can simply navigate to the page next to the login page, if the cookie exists.
                    //Option 1:
                    //Display the credentials (Username & Password) on the login page.
                    //Pass the Username and Password to the respective TextBox.

                    txtUsername.Text = Request.Cookies["C_LOGIN"]["Username"].ToString();
                    txtPassword.Attributes["value"] = Request.Cookies["C_LOGIN"]["Password"].ToString();
                    cbRememberMe.Checked = true;
                    
                  	//OR
                    //Option 2:
                    //Navigate to home page without displaying login page.
                    //Response.Redirect("home.aspx");
                }
            }
        }
    }
  	catch (Exception ex) {}
  	finally {}
}

Button Click Event (Login Button)

protected void btnLogin_Click(object sender, EventArgs e)
{
    try
    {
        //Save login credentials in cookies.
        //Note: "C_LOGIN" is a user defined name.
        //Check if "Remember Me" checkbox is checked on login.
        
      	if (cbRememberMe.Checked)
        {
            if (Request.Browser.Cookies)
            {
                //Create a cookie with expiry of 60 days.
                Response.Cookies["C_LOGIN"].Expires = DateTime.Now.AddDays(60);
              
                //Write Username to the cookie.
                Response.Cookies["C_LOGIN"]["Username"] = txtUsername.Text.Trim();
              
                //Write Password to the cookie.
                Response.Cookies["C_LOGIN"]["Password"] = txtPassword.Text.Trim();
            }

            //If the cookie already exists then write the Username and Password to the cookie.
            else
            {
                Response.Cookies["C_LOGIN"]["Username"] = txtUsername.Text.Trim();
                Response.Cookies["C_LOGIN"]["Password"] = txtPassword.Text.Trim();
            }
        }
      	else
        {
            //If the checkbox is unchecked then clean the cookie "C_LOGIN"
            Response.Cookies["C_LOGIN"].Expires = DateTime.Now.AddDays(-1);
        }
    }
  	catch (Exception ex) {}
  	finally {}
}