Friday, January 25, 2013

Remember password functionality in ASP.NET

In the Login button click write the below code

protected void btn_Login_Click(object sender, EventArgs e)  
{  
     if (cb_Remember.Checked)  
     {  
          HttpCookie prCookie = new HttpCookie("PRCOOKIE");  
          prCookie["EID"] = txt_EmpId.Text;  
          prCookie["PWD"] = txt_Password.Text;  
          Response.Cookies.Add(prCookie);  
          Response.Cookies["PRCOOKIE"].Expires = DateTime.Now.AddDays(30);  
     }  
     else  
     {  
          Response.Cookies["PRCOOKIE"].Expires = DateTime.Now.AddDays(-1);  
     }  
}  
Here,
cb_Remember is a CheckBox
txt_EmpId.Text is a Login Name TextBox
txt_Password.Tex is a Password TextBox


then in the Page_Load function add the below code

protected void Page_Load(object sender, EventArgs e)  
{  
     HttpCookie cookie = Request.Cookies["PRCOOKIE"];  
     if (cookie != null)  
     {  
          txt_EmpId.Text = Convert.ToString(cookie["EID"]);  
          txt_Password.Attributes["Value"] = Convert.ToString(cookie["PWD"]);  
     }  
}