in my application. there is a registration mechanism that saves a cookie with the information of the user who has just logged in
private void CreateCookie(LoginEventArgs args) { HttpCookie cookie = new HttpCookie("user"); cookie.Values["name"] = args.User_Name; cookie.Values["id"] = args.ID; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); }
on my loading of the homepage, I am checking to see if this cookie exists:
HttpCookie cookie = Request.Cookies["user"] ; if( (cookie != null) && (cookie.Value != "")) { if (Session["user"] == null) Login_Passed(this, new LoginEventArgs(cookie.Values["name"].ToString(), int.Parse(cookie.Values["id"]))); }
Now, if I write (create a cookie), close the browser and launch the application. again, does the cookie exist correctly, and the user "automatically" logs in.
if I first redirect to another content page from the content start page, the cookie values are also not corrupted,
the problem is that I redirect back to another content page a second time, loading the main page, does the cookie check exist, but the values are deleted ...
any ideas on why this is happening?
btw, perhaps the reason for overcoming this problem may be:
When I log out, I create a cookie with the same name that expires 1 day ago.
private void Remove_Cookie() { HttpCookie cookie = new HttpCookie("user"); cookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(cookie); }
in case iv'e it is described that I do not log out formally, I just end my application, so this should not have any effect.