Friday, January 25, 2013

Remember password functionality in ASP.NET

In the Login button click write the below code

  1. protected void btn_Login_Click(object sender, EventArgs e)
  2. {
  3. if (cb_Remember.Checked)
  4. {
  5. HttpCookie prCookie = new HttpCookie("PRCOOKIE");
  6. prCookie["EID"] = txt_EmpId.Text;
  7. prCookie["PWD"] = txt_Password.Text;
  8. Response.Cookies.Add(prCookie);
  9. Response.Cookies["PRCOOKIE"].Expires = DateTime.Now.AddDays(30);
  10. }
  11. else
  12. {
  13. Response.Cookies["PRCOOKIE"].Expires = DateTime.Now.AddDays(-1);
  14. }
  15. }
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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. HttpCookie cookie = Request.Cookies["PRCOOKIE"];
  4. if (cookie != null)
  5. {
  6. txt_EmpId.Text = Convert.ToString(cookie["EID"]);
  7. txt_Password.Attributes["Value"] = Convert.ToString(cookie["PWD"]);
  8. }
  9. }

No comments:

Post a Comment