简体   繁体   English

HTTPOnly将cookie过期设置为会话

[英]HTTPOnly sets cookie expiration to session

I am writing a "Remember My Username" Cookie that expires in a custom duration of time eg one month. 我正在编写“记住我的用户名” Cookie,该Cookie在自定义的持续时间(例如一个月)内到期。 I noticed that when I add HttpOnly = true, the expiration changes to session. 我注意到当我添加HttpOnly = true时,到期更改为会话。 Why is this? 为什么是这样? I can't seem to find any documentation on why this would happen. 我似乎找不到任何有关为什么会发生这种情况的文档。

Thanks. 谢谢。

Here is the documentation. 是文档。

true if the cookie has the HttpOnly attribute and cannot be accessed through a client-side script; 如果cookie具有HttpOnly属性并且无法通过客户端脚本访问,则为true;否则为true。 otherwise, false. 否则为假。 The default is false. 默认为false。

Basically, it becomes a session variable because it will only be stored on the server due to your setting 基本上,它成为一个会话变量,因为由于您的设置,它将仅存储在服务器上

I'm adding the following code: Also, now I'm getting a different behaviors than the Title. 我添加以下代码:此外,现在我得到的行为与标题不同。 I'm running this locally against the VS2010 built-in server. 我正在针对VS2010内置服务器在本地运行它。 It seems to show inconsistent behaviors. 它似乎显示出不一致的行为。 I would move the HttpOnly = true before the Expires and after it and it seemed to change behavior until I refreshed the browser page. 我会将HttpOnly = true移到Expires之前和之后,在我刷新浏览器页面之前,它似乎改变了行为。 So, I am assuming everything was fine and never had an issue. 因此,我认为一切都很好,并且从未出现过问题。 In addition, I am moving HttpOnly and Secure flags to the web.config because not all my environments have SSL. 另外,我将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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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