Login Page With Remember Password Code In ASP. Net 4.0

In This article I am going to explain how to implement Remember Me checkbox functionality and simple login page code in asp. net. Remember me checkbox will maintain username and password for user when user visits next time.

To create the simple login page with remember password do the following.

First of all open a web site in ASP.Net and add some simple ASP.Net code within the default.aspx page. For example I must insert two TextBoxes, one for the userid and another for the password and one checkbox control that allows the user to specify whether he wants the page to remember the username and password when he visits the next time and one login button.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    Userid:

    <asp:TextBox ID ="textbox1" runat ="server" ></asp:TextBox><br />

    Password:

    <asp:TextBox ID ="textbox2" runat ="server" TextMode ="Password"  ></asp:TextBox><br />

    <asp:CheckBox ID ="rememberme" runat="server"  Text ="remember Password" /><br />

    <asp:Button ID ="Login" runat ="server" Text ="Login" onclick="Login_Click" />

    </form>

</body>

Save Userid and Password in cookie

Now the login button is clicked where I have login page code that stores the userid and password in a cookie. To do that, first dtermine if the remember me checkbox is checked. If it is checked then store the username and password into the cookie for the next 15 days from the current date. And if not checked then set the expiration date to 1 day in the future to destroy the cookie.

Login button click code.

    protected void Login_Click(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection("Data Source=MCNDESKTOP08;Initial Catalog=pulkit;User ID=sa;Password=wintellect");

        SqlCommand cmd = new SqlCommand("select userid, pwd from logdetail where userid='" + textbox1.Text + "' and pwd='" + textbox2.Text + "'", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill (ds );
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (rememberme.Checked == true)
            {
                Response.Cookies["userid"].Value = textbox1.Text;
                Response.Cookies["pwd"].Value = textbox2.Text;
                Response.Cookies["userid"].Expires = DateTime.Now.AddDays(15);
                Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(15);
             }

            else

            {

                Response.Cookies["userid"].Expires = DateTime.Now.AddDays(-1);

                Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(-1);

            }

            Response.Redirect("user.aspx");

        }

        else

        {

            Response.Write("invalid username and password");

        }

    }

Now Fetch the Userid and Password from the Cookie and Set it in Textboxes.

In this step I will check if the Cookie exists. Then I will load the Userid and Password into each respective TextBox in the page_Load event.

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            if (Request.Cookies["userid"] != null)

                textbox1.Text = Request.Cookies["userid"].Value;

            if (Request.Cookies["pwd"] != null)

                textbox2.Attributes .Add ("value", Request.Cookies["pwd"].Value);
            if (Request.Cookies["userid"] != null && Request.Cookies["pwd"] != null)
                rememberme.Checked = true;
        }
    }


Note: After implementing this code your password will be stored in the browser for 15 days. When you enter the email id the password will automatically appear.