饼干过期不工作在C# [英] Cookies expiration not working in C#

查看:123
本文介绍了饼干过期不工作在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用C#4.0做一个永久性的Cookie,这code:

 的HttpCookie AssoCookie =新的HttpCookie(准code,副code);
AssoCookie.Expires = DateTime.Now.AddMonths(6);
HttpContext.Current.Response.Cookies.Add(AssoCookie);
 

不幸的是,它不工作的cookie是会话届满。如果我使用Web开发的插件在Firefox来检查它,我可以看到这样的信息:

 名助理code
值测试
主机本地主机
路径    /
安全无
到期结束会话
 

我在做一个试验的cookie它只是在那里创建,否则没有人管理它或编辑它,所以它不能被覆盖。在webconfig设置Cookie到期我什么都没有设置(反正这应该重写),如果我设定了一些饼干过期时间是行不通的。

我现在有点失落,每一个手册,指南,博客,无论我查说,你只需要设置的过期日期,我已经调试,香港专业教育学院检查创建时,它确实具有价值和PAGE_年底preRender但它只是到期与会话无论我做什么。

任何想法?

即时通讯使用的是Firefox 9.0.1 BTW。

更新

在Firefox用Firebug插件来检查响应头,我得到这样的:

 响应Headersview源
缓存控制私人
连接关闭
内容长度21322
内容类型text / html;字符集= UTF-8
日期星期一,2012 16时47分08秒格林尼治标准​​时间1月23日
服务器ASP.NET开发服务器/ 10.0.0.0
设置Cookie副code =测试;到期=周一,23月2012年16点○九分04秒格林尼治标准​​时间; PATH = /
的X ASPNET-版本4.0.30319
 

和使用招我得到这样的:

  HTTP / 1.1 200 OK
缓存控制:私人
内容类型:text / html的;字符集= UTF-8
服务器:Microsoft-IIS / 7.5
的X ASPNET-版本:4.0.30319
设置Cookie:大专code =;到期=周一,23月 -  2012格林尼治标准​​时间16时27分29秒; PATH = /
的X已启动方式:ASP.NET
日期:星期一,2012 17时27分29秒格林尼治标准​​时间1月23日
内容长度:21313
 

**更新2 ** 林检查Cookie的犯规存在只是我的Firefox的饼干之间的检查,但是,如果你想看到我在code以后得到它(没有必要,因为它不是在那里反正):

 尝试{
    副code = HttpContext.Current.Request.Cookies.Get(准code)的价值。
} 抓住 { }
 

解决方案

好了各位。我很抱歉,但似乎由于某种原因,我已经配置了我的浏览器前段时间删除cookie,当我关闭的窗口,所以,尽管得到适当创建的cookie的浏览器设置好的它们作为会话cookie,不是因为他们不过是因为浏览器选项。

事实上,如果我去了在Firefox中管理Cookie,我可以取消在cookie的选项会话Cookie,然后在截止日期是正确的,但由于某些原因,检查选项默认为选中。我想,也许有一种方式,如果一个Cookie是会话Cookie或没有,或者净是做什么,直到我意识到这一点来设置。

嗯,没事的时候逻辑似乎工作,你知道这个问题是最愚蠢的事,你能想象...

再次抱歉。

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

解决方案

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.

这篇关于饼干过期不工作在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆