HTTPOnly将cookie过期设置为会话

问题描述:

我正在写一个“记住我的用户名”Cookie,并在自定义的持续时间内过期。一个月。我注意到,当我添加HttpOnly = true时,到期会变为会话。为什么是这样?我似乎无法找到任何有关这种情况发生的文件。HTTPOnly将cookie过期设置为会话

谢谢。

+0

欢迎堆栈溢出。请记住,赞赏是通过upvotes和接受的答案来显示的。我强烈建议所有新用户仔细阅读[faq],尤其是[ask] :) – 2012-03-10 19:41:09

+0

您能告诉我们您用来设置和阅读cookie的代码吗? – LukeH 2012-03-10 22:47:14

我添加以下代码:还有,现在我就比一个不同的行为标题。我在VS2010内置服务器上本地运行。它似乎表现出不一致的行为。我会在Expires之前和之后移动HttpOnly = true,它似乎会改变行为,直到我刷新浏览器页面。所以,我假设一切都很好,从来没有问题。另外,我将HttpOnly和Secure标志移至web.config,因为并非我所有的环境都具有SSL。


FormsAuthenticationTicket ticket = new FormsAuthenticationTicket 
               (strUserID, //name 
               false, //IsPersistent 
               24 * 60); // 24 hours 

// Encrypt the ticket. 
string encryTicket = FormsAuthentication.Encrypt(ticket); 

// Create the cookie. 
HttpCookie userCookie = new HttpCookie("Authentication", encryTicket); 
userCookie.HttpOnly = true; 
Response.Cookies.Add(userCookie); 

e.Authenticated = true; 
if (LoginPannelMain.RememberMeSet) 
{ 
    HttpCookie aCookie = new HttpCookie("email", strUserLogin); 
    aCookie.HttpOnly = true; 
    aCookie.Expires = DateTime.Now.AddYears(1); 
    Response.AppendCookie(aCookie); 
} 
else 
{ 
    HttpCookie aCookie = new HttpCookie("email", ""); 
    aCookie.HttpOnly = true; 
    Response.AppendCookie(aCookie); 
} 
+0

我想通了,我没有选中复选框,在代码中有过期代码导致我原来的问题。 – evodev 2012-03-13 23:45:22

Here是文档。

如果cookie具有HttpOnly属性且无法通过客户端脚本访问 ,则为true;否则,是错误的。默认值是false。

基本上,它变成一个会话变量,因为它只会被存储在服务器上,由于你的设置