Full Code For Login With Remember Me In Asp.Net C#

Here i am going to  tell you how to create login page with remember me checkbox with simple code, remember me checkbox will keep username and password for user to login next time without putting user id and password

Here is aspx page and codebehind code with database.

.aspx page code


<div class="sign-in-form">

<asp:TextBox TextMode="Email" ID="txtemail" runat="server" class="form-box required" placeholder="Email Address" required></asp:TextBox>

<asp:TextBox ID="txtpassword" TextMode="Password" runat="server" class="form-box required" placeholder="Password" required></asp:TextBox>
                                   
<div class="sign-in-settings">

<input runat="server" id="chkrembrme" type="checkbox" name="newsletter" value="1">

<span>Remember me</span>

<a href="#">Lost your password?</a>

</div>

<asp:Button ID="btnSignin" runat="server" class="submit-button" Text="Signin" onclick="btnSignin_Click" />

</div>




following code in code behind

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["userid"] != null)
                {
                    txtemail.Text = Request.Cookies["userid"].Value;
                
                }
                if(Request.Cookies["password"]!= null)
                {
                       txtpassword.Attributes.Add("value", Request.Cookies["password"].Value);
                }
                if (Request.Cookies["userid"] != null && Request.Cookies["password"] != null)
                {
                    chkrembrme.Checked = true;
                }
            }

          
        }

        protected void btnSignin_Click(object sender, EventArgs e)
        {
           
            string cstring = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            SqlConnection con = new SqlConnection(cstring);
            con.Open();
            SqlCommand cmd = new SqlCommand("signin",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@userid", txtemail.Text);
            cmd.Parameters.AddWithValue("@password", txtpassword.Text);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                if (chkrembrme.Checked)
                {
                    Response.Cookies["userid"].Value = txtemail.Text;
                    Response.Cookies["password"].Value =        txtpassword.Text;
                    Response.Cookies["userid"].Expires = DateTime.Now.AddDays(15);
                    Response.Cookies["password"].Expires = DateTime.Now.AddDays(15);
                }

                Response.Redirect("index.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
            }
             

               

        }

Output



Comments

Popular posts from this blog

Disable right click, Inspect Element and page source using JavaScript in Html and asp.net