Cookies expiration not working in C#

22,534

Solution 1

Ok everybody. I am so sorry but it seems for some reason I had configured my browsers time ago to delete cookies when I closed windows, so, despite Cookies were being created properly the browser setted them as session cookies not because they were but because of browser options.

In fact, if I went to manage Cookies in Firefox, I could uncheck the option "Session Cookie" in a cookie and then the Expiration Date was the correct one, but for some reason that check option was checked by default. I thought that maybe there was a way to set if a Cookie is Session Cookie or not, or maybe .Net was doing something until I realized this.

Well, when nothing logic seems to work, you know the problem is the most stupid thing you could imagine...

Sorry again.

Solution 2

Just to follow up with my comment:

The info from fiddler and firefox's tools show that you are sending the correct information to create the cookie.

This leads me to believe that you are incorrectly testing it's existence.

When testing if a cookie is passed to your application you should use the Request.Cookies container to see if it's there. (note that it is REQUEST not RESPONSE).

If you test by using something like:

if (Response.Cookies["mycookie"] == null) { .. }

Then this will cause a new cookie in the response object to be created that is blank.

To sum up: the Request object will contain everything sent from the browser. The Response object is everything you are sending to the browser.

Solution 3

Have you tried using the SetCookie function instead of Add?

HttpCookie cookie = new HttpCookie("test");
cookie.Expires = DateTime.Now.AddHours(10);
HttpContext.Current.Response.SetCookie(cookie);

Just tested this quickly and it works for me.

Share:
22,534
Ruben.Canton
Author by

Ruben.Canton

Updated on January 25, 2020

Comments

  • Ruben.Canton
    Ruben.Canton over 4 years

    Im trying to make a persistent cookie using C# 4.0 and this code:

    HttpCookie AssoCookie = new HttpCookie("AssociateCode", AssociateCode);
    AssoCookie.Expires = DateTime.Now.AddMonths(6);
    HttpContext.Current.Response.Cookies.Add(AssoCookie);
    

    Unfortunately, it doesn't work and the cookie is session expiring. If I use Web Developer plugin in Firefox to check it I can see this info:

    Name    AssociateCode
    Value   test
    Host    localhost
    Path    /
    Secure  No
    Expires At End Of Session
    

    The cookie I'm doing as a test its just created on there, noone else manages it or edits it so it cannot be overwritten. I have nothing set in webconfig to set cookies expiration (anyway this should override that) and if I set some cookies expiration time it doesnt work.

    I'm a bit lost now, every manual, tutorial, blog, whatever I check says you just have to set the Expiration date, Ive debugged, Ive checked that it really has that value when created and at the end of Page_PreRender but it just expires with session whatever I do.

    Any ideas?

    Im using Firefox 9.0.1 btw.

    Update

    Using Firebug plugin in Firefox to check response headers I get this:

    Response Headersview source
    Cache-Control   private
    Connection  Close
    Content-Length  21322
    Content-Type    text/html; charset=utf-8
    Date    Mon, 23 Jan 2012 16:47:08 GMT
    Server  ASP.NET Development Server/10.0.0.0
    Set-Cookie  AssociateCode=test; expires=Mon, 23-Jul-2012 16:09:04 GMT; path=/
    X-AspNet-Version    4.0.30319
    

    And using Fiddler I get this:

    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Type: text/html; charset=utf-8
    Server: Microsoft-IIS/7.5
    X-AspNet-Version: 4.0.30319
    Set-Cookie: AssociateCode=; expires=Mon, 23-Jul-2012 16:27:29 GMT; path=/
    X-Powered-By: ASP.NET
    Date: Mon, 23 Jan 2012 17:27:29 GMT
    Content-Length: 21313
    

    ** Update 2 ** Im checking that the Cookie doesnt exist just checking between my Firefox's cookies, but, in case you want to see how I get it later in code (not necessary since it not on there anyway):

    try {
        AssociateCode = HttpContext.Current.Request.Cookies.Get("AssociateCode").Value;
    } catch { }
    
  • Ruben.Canton
    Ruben.Canton over 12 years
    Nice try, Ive checked it and doesnt work. It has to be some type of IIS config or something im missing. Can someone at least confirm me that this should work? Im starting to doubt it now...
  • Sam Greenhalgh
    Sam Greenhalgh over 12 years
    Yes, this should work. Can you post the HTTP response (Set-Cookie header) so we can see exactly what the server is emitting?
  • Ruben.Canton
    Ruben.Canton over 12 years
    Yeah, thats a nice tip, it can happen sometimes. But I know the difference between Request and Response and this is not the case. In fact, I only need to close and open the window, search for the cookie in Firefox's Cookies and it is disappeared, or using Web Developer plugin, it doesnt exist. So, I dont even need to check if it exists using .Net, and if I do, I use Request instead of course. || Ex: try { AssociateCode = Current.Request.Cookies.Get("AssociateCode").Value; } catch { }
  • Bengi Besçeli
    Bengi Besçeli over 6 years
    No problem for me, I have the same error and now I can try these solutions, thanks
  • Bengi Besçeli
    Bengi Besçeli over 6 years
    SetCookie is undefined, did you mean using Set rather than SetCookie?