FormsAuthenticationTicket isPersistent属性的目的是什么?

Chr*_*ray 20 c# asp.net authentication

我正试着绕着班上isPersistent发现的财产的目的FormsAuthenticationTicket.http://msdn.microsoft.com/en-us/library/kybcs83h.aspx

  1. 是否存在设置isPersistent工作的情况?
  2. 在什么情况下我想设置isPersistent为true和false?

该属性似乎是多余的,因为我发现在浏览器会话中持久保存用户身份验证cookie的唯一方法是设置Expires创建故障单后创建的cookie 的属性; 即使门票的持久值设置为false.

我还发现将票证到期(不是cookie)isPersistent设置为10秒,设置为true几乎没有效果; 门票在10秒后到期.

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
    identity.Name,
    DateTime.Now,
    DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
    isPersistent,
    JsonSerializerService.ToJson(identity),
    FormsAuthentication.FormsCookiePath);

string encryptedTicket = FormsAuthentication.Encrypt(ticket);

var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

cookie.Path = FormsAuthentication.FormsCookiePath;

cookie.Expires = DateTime.Now.AddYears(1); // good for one year
Run Code Online (Sandbox Code Playgroud)

我很欣赏我可以将上面的代码更改为可选的设置 expires

if (isPersistent)
    cookie.Expires = DateTime.Now.AddYears(1); // good for one year
Run Code Online (Sandbox Code Playgroud)

已经创建了一个示例应用程序@ GitHub.https://github.com/chrismoutray/AuthSample这基本上表明,即使将isPersistent标志设置为true,跨浏览器授权也不起作用.

lnu*_*lnu 10

在框架1.0/1.1中,将IsPersistent设置为true会将cookie的有效期设置为50年.
在版本2.0中,它已更改,因此cookie的过期与表单身份验证超时属性匹配.因此,您可以将IsPersistent设置为true,但cookie将始终在表单身份验证超时期限后过期.
如果您需要较长的有效期而不修改表单身份验证超时,则您的代码可以解决问题.

编辑:我已经下载了您的示例并替换了您的cookie代码

 FormsAuthentication.SetAuthCookie(model.UserName, true);
Run Code Online (Sandbox Code Playgroud)

并且它按预期工作:将两天配置为表单超时,我的cookie将在两天后过期.