我正在编写一个“记住我的用户名”Cookie,该 Cookie 在自定义持续时间(例如一个月)内到期。我注意到当我添加 HttpOnly = true 时,过期更改为会话。为什么是这样?我似乎找不到任何关于为什么会发生这种情况的文档。
谢谢。
我正在添加以下代码:另外,现在我得到的行为与标题不同。我在 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);
}