Cookies disappear when going between content pages

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.

+3
c # cookies
Sep 22 '11 at 6:13
source share
3 answers

o'k, the problem was unthinkable
special thanks to Peter Bromberg

http://www.eggheadcafe.com/tutorials/aspnet/198ce250-59da-4388-89e5-fce33d725aa7/aspnet-cookies-faq.aspx

in the section of the article "Disappearing cookies"

the author claims that if you have a Response.Cookies ["cookie_name"] clock, the browser creates a new empty cookie that overrides your cookie.

I used a watch that made my cookie lose its values, and when I removed it from the cookie, it saved its values.

morality is NOT WATCHING DENIAL. Cookies [""] I also read in another post that if you check

  if( Response.Cookies["cookie_name"] != null ) 

for example, it is also redefined.

+13
Sep 23 2018-11-21T00:
source share

Repeat and rely on what has already been indicated (yes, I know this is a 4-year issue). I found that it is best to create a utility to handle this - mainly because I want to check this particular cookie often.

This will not affect the response, but only read the request.

  public static HttpCookie GetCookie(string cookieName) { HttpCookie rqstCookie = HttpContext.Current.Request.Cookies.Get(cookieName); /*** NOTE: it will not be on the Response! * this will trigger the error noted in the original question and * create a new, empty cookie which overrides it * HttpCookie respCookie = HttpContext.Current.Response.Cookies.Get(cookieName); * */ if (rqstCookie != null && !String.IsNullOrEmpty(rqstCookie.Value)) { // is found on the Request return rqstCookie; } else { return null; } } 

rule of thumb

Always read from the request and write in response.

Thanks Eran! this post was exactly what I needed

+6
Dec 22 '15 at 17:49
source share

try the following:

  • If you are developing your local machine, place your application on some free web page, so there will be no special treatment because you are on the local computer.
  • If you are already on a web server, and if the redirect is between towing different domains, you can search google for "the same origin policy" or read the following: http://en.wikipedia.org/wiki/Same_origin_policy (the document talks about javascript, but it also applies to cookies).
0
Sep 22 '11 at 7:24 a.m.
source share



All Articles