Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Star
10850 Points
2098 Posts
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>
protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["myCookie"] != null) { HttpCookie cookie = Request.Cookies.Get("myCookie"); String UserName = cookie.Values["username"]; ///CheckBox rm = (CheckBox)ctlLogin.FindControl("RememberMe"); Response.Write(UserName); } // Note this Response.Cache.SetNoStore(); } protected void Button1_Click(object sender, EventArgs e) { HttpCookie myCookie = new HttpCookie("myCookie"); //Response.Cookies.Remove("myCookie"); Response.Cookies.Add(myCookie); //myCookie.Values.Add("username", this.ctlLogin.UserName); myCookie.Values.Add("username", "the cookie username"); DateTime dtExpiry = DateTime.Now.AddDays(15); //you can add years and months too here Response.Cookies["myCookie"].Expires = dtExpiry; } protected void Button2_Click(object sender, EventArgs e) { HttpCookie cook = new HttpCookie("myCookie"); cook.Expires = DateTime.Now.AddSeconds(5); Response.Cookies.Add(cook); }
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.