Hi there..I am having some trouble implementing the "Remember Me" check box on my asp:login control.
I am able to set the cookie when the user checks the box, and I can read the cookie and pre-populate the userid field, but I can't automatically check the checkbox...
Can someone tell me what I am doing wrong?
TextBox txtUser = ctlLogin.FindControl("UserName") as TextBox;
if (!IsPostBack)
{
if (Request.Cookies["myCookie"] != null)
{
HttpCookie cookie = Request.Cookies.Get("myCookie");
ctlLogin.UserName = cookie.Values["username"].ToString();
CheckBox rm = (CheckBox)ctlLogin.FindControl("RememberMe");
rm.Checked=true;
}
this.SetFocus(txtUser);
}
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.
I don't see anything wrong with your code except that you might not be refreshing (setting caching), and or goin to another page and then comming back.
smithygreg
Member
2 Points
10 Posts
ASP:Login Remember Me functionality
Mar 15, 2007 03:25 PM|LINK
Hi there..I am having some trouble implementing the "Remember Me" check box on my asp:login control.
I am able to set the cookie when the user checks the box, and I can read the cookie and pre-populate the userid field, but I can't automatically check the checkbox...
Can someone tell me what I am doing wrong?
TextBox txtUser = ctlLogin.FindControl("UserName") as TextBox; if (!IsPostBack) { if (Request.Cookies["myCookie"] != null) { HttpCookie cookie = Request.Cookies.Get("myCookie"); ctlLogin.UserName = cookie.Values["username"].ToString(); CheckBox rm = (CheckBox)ctlLogin.FindControl("RememberMe"); rm.Checked=true; } this.SetFocus(txtUser); }Thanks
Greg
ASP.Net Login
smithygreg
Member
2 Points
10 Posts
Re: ASP:Login Remember Me functionality
Mar 15, 2007 04:18 PM|LINK
Um..I guess I should mention that this code is in the PageLoad method...
Thanks again..
-Greg
mokeefe
Star
10850 Points
2098 Posts
Re: ASP:Login Remember Me functionality
Mar 15, 2007 04:33 PM|LINK
Can you post where you are appending the cookie to the output response OnLogin etc.
Martin.
For the benefit of all users please mark any post answers as appropriate.
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
mokeefe
Star
10850 Points
2098 Posts
Re: ASP:Login Remember Me functionality
Mar 15, 2007 08:08 PM|LINK
I don't see anything wrong with your code except that you might not be refreshing (setting caching), and or goin to another page and then comming back.
Here was my test -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestCookie.aspx.cs" Inherits="ForumsStuff_CSharp_TestCookie" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Set Cookie" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="Remove Cookie" OnClick="Button2_Click"/> </div> </form> </body> </html>Martin.
For the benefit of all users please mark any post answers as appropriate.
smithygreg
Member
2 Points
10 Posts
Re: ASP:Login Remember Me functionality
Mar 15, 2007 09:14 PM|LINK
Hey Martin...
I am having no trouble reading the cookie...I am having trouble getting the check box to be checked if that cookie exists...
CheckBox rm = (CheckBox)ctlLogin.FindControl("RememberMe"); rm.Checked=true;It's this part of the code that doesn't seem to be working...
Can you stick that in you app and see if it works?
I added the Response.Cache.SetNoStore() and it didn't seem to work.
thanks
Greg
mokeefe
Star
10850 Points
2098 Posts
Re: ASP:Login Remember Me functionality
Mar 16, 2007 04:03 AM|LINK
Martin.
For the benefit of all users please mark any post answers as appropriate.
mokeefe
Star
10850 Points
2098 Posts
Re: ASP:Login Remember Me functionality
Mar 16, 2007 04:28 AM|LINK
protected void Page_Init(object sender, EventArgs e) { if (!IsPostBack) { if (Request.Cookies["myCookie"] != null) { HttpCookie cookie = Request.Cookies.Get("myCookie"); ctlLogin.UserName = cookie.Values["username"]; ctlLogin.RememberMeSet = (!String.IsNullOrEmpty(ctlLogin.UserName)); } TextBox txtUser = ctlLogin.FindControl("UserName") as TextBox; if(txtUser != null) this.SetFocus(txtUser); } // Note this Response.Cache.SetNoStore(); } protected void ctlLogin_LoggedIN(object sender, EventArgs e) { HttpCookie myCookie = new HttpCookie("myCookie"); Boolean remember = ctlLogin.RememberMeSet; if (remember) { Int32 persistDays = 15; myCookie.Values.Add("username", ctlLogin.UserName); myCookie.Expires = DateTime.Now.AddDays(persistDays); //you can add years and months too here } else { myCookie.Values.Add("username", string.Empty); // overwrite empty string is safest myCookie.Expires = DateTime.Now.AddMinutes(5); //you can add years and months too here } Response.Cookies.Add(myCookie); }Martin.
For the benefit of all users please mark any post answers as appropriate.
lernendotnet
Member
56 Points
25 Posts
Re: ASP:Login Remember Me functionality
Mar 16, 2007 10:21 AM|LINK
is it possible for you to give dsign code of checkbox control in your page?
are you creating checkbox control in design or dynamically through code?
Dont Forget to mark the reply as ANSWER if you could solve the condition successfully.
mokeefe
Star
10850 Points
2098 Posts
Re: ASP:Login Remember Me functionality
Mar 16, 2007 10:25 AM|LINK
This is all there is -
<form id="form1" runat="server"> <div> <asp:Login ID="ctlLogin" runat="server"> </asp:Login> </div> </form>Martin.
For the benefit of all users please mark any post answers as appropriate.