Wednesday, September 26, 2007

"Remember User Login" using ASP.NET,C#.NET

// Write this code in the Login button click
// Store the user's username and password in a cookie when checkbox Remember Me checked
if (chkRememberLogin.Checked == true)
{
HttpCookie myCookie = new HttpCookie("myCookie");
Response.Cookies.Remove("myCookie");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("UserName", this.txtUserID.Text.ToString());
myCookie.Values.Add("PassWord", this.txtPassword.Text.ToString());
DateTime dtExpiry = DateTime.Now.AddMonths(1); //you can add years and months too here Response.Cookies["myCookie"].Expires = dtExpiry;
}

// Write this code in the Form Load()
if (!IsPostBack)
{

if (Request.Cookies["myCookie"] != null)
{
HttpCookie myCookie = Request.Cookies.Get("myCookie");
this.txtUserID.Text = myCookie.Values["UserName"].ToString();
this.txtPassword.Attributes.Add("Value", myCookie.Values["PassWord"].ToString());
}
}

No comments: