protected void ctlLogin_LoggedIN(object sender, EventArgs e)
{
CheckBox rm = (CheckBox)ctlLogin.FindControl("RememberMe");
if (rm.Checked)
{
HttpCookie myCookie = new HttpCookie("myCookie");
Response.Cookies.Remove("myCookie");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("username", this.ctlLogin.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddDays(15); //you can add years and months too here
Response.Cookies["myCookie"].Expires = dtExpiry;
}
else
{
HttpCookie myCookie = new HttpCookie("myCookie");
Response.Cookies.Remove("myCookie");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("username", this.ctlLogin.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here
Response.Cookies["myCookie"].Expires = dtExpiry;
}
}
This function is called after the applicaton verifies that the user is a valid user and is logged in. I tried to just remove the cookie if the checkbox wasn't checked, but it didn't seem to be working, so I just give it a very short expiration date to get
rid of it.
smithygreg
Member
2 Points
10 Posts
Re: ASP:Login Remember Me functionality
Mar 15, 2007 04:48 PM|LINK
protected void ctlLogin_LoggedIN(object sender, EventArgs e) { CheckBox rm = (CheckBox)ctlLogin.FindControl("RememberMe"); if (rm.Checked) { HttpCookie myCookie = new HttpCookie("myCookie"); Response.Cookies.Remove("myCookie"); Response.Cookies.Add(myCookie); myCookie.Values.Add("username", this.ctlLogin.UserName.ToString()); DateTime dtExpiry = DateTime.Now.AddDays(15); //you can add years and months too here Response.Cookies["myCookie"].Expires = dtExpiry; } else { HttpCookie myCookie = new HttpCookie("myCookie"); Response.Cookies.Remove("myCookie"); Response.Cookies.Add(myCookie); myCookie.Values.Add("username", this.ctlLogin.UserName.ToString()); DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here Response.Cookies["myCookie"].Expires = dtExpiry; } }This function is called after the applicaton verifies that the user is a valid user and is logged in. I tried to just remove the cookie if the checkbox wasn't checked, but it didn't seem to be working, so I just give it a very short expiration date to get rid of it.
Once again..thanks for any help!
Greg