简体   繁体   中英

Cookies expiration not working in C#

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 { }

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM