3

我正在编写一个“记住我的用户名”Cookie,该 Cookie 在自定义持续时间(例如一个月)内到期。我注意到当我添加 HttpOnly = true 时,过期更改为会话。为什么是这样?我似乎找不到任何关于为什么会发生这种情况的文档。

谢谢。

4

2 回答 2

2

是文档。

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

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

于 2012-03-10T19:30:09.490 回答
0

我正在添加以下代码:另外,现在我得到的行为与标题不同。我在 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);
}
于 2012-03-13T18:18:36.040 回答