问题描述
我试图让我的头轮上的的FormsAuthenticationTicket
类中找到的在 isPersistent
财产的目的。 http://msdn.microsoft.com/en-us/library/kybcs83h.aspx
I'm trying to get my head round the purpose of the isPersistent
property found on the FormsAuthenticationTicket
class. http://msdn.microsoft.com/en-us/library/kybcs83h.aspx
- 是否有场景时设置isPersistent工作?
- 在什么情况下我会想设置
isPersistent
真假?
- Are there scenarios when setting isPersistent works?
- In what scenarios would I want to set
isPersistent
to true and false?
属性似乎是多余的,因为我发现我的唯一办法坚持跨浏览器会话我的用户身份验证cookie是设置过期的cookie
属性创建以下票创作;即使门票isPersistent值设置为假
。
The property seems to be redundant since I've found the only way for me to persist my users authentication cookie across browser sessions is to set the Expires
property of the cookie created following ticket creation; even if the tickets isPersistent value is set to false
.
我还发现,设置门票有效期(不是饼干),以像10秒钟 isPersistent
设置为true影响不大;门票10秒后过期。
I also found that setting the tickets expiry (not the cookie) to something like 10 seconds with isPersistent
set to true has little effect; the ticket expires after 10 seconds.
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
我AP preciate,我可以改变我的上述code如果选择设置终止
if (isPersistent)
cookie.Expires = DateTime.Now.AddYears(1); // good for one year
这是示例应用程序已创建@ GitHub上。 这基本上表明,即使在isPersistent标志设置为true,跨浏览器的授权没有按'T的工作。
An example application has been created @ GitHub. https://github.com/chrismoutray/AuthSample This basically shows that even by setting the isPersistent flag to true the cross browser authorization doesn't work.
推荐答案
在框架1.0 / 1.1,设置IsPersistent为true,将设置50年到cookie到期。结果
在2.0版本,它已更改,以便该cookie的过期形式认证超时属性相匹配。所以,你可以设置IsPersistent为true,但形式认证超时时间后,cookie将始终过期。结果
如果你想在不修改窗体身份验证超时长保质期的code的伎俩。
In framework 1.0/1.1, setting IsPersistent to true would set an expiration of 50 years to the cookie.
In version 2.0 it was changed so the expiration of the cookie matches the form authentication timeout attribute. So you can set IsPersistent to true but the cookie will always expire after the form authentication timeout period.
Your code does the trick if you want long expiration period without modifying forms authentication timeout.
编辑:我已经下载了您的样品,以
edit: I've downloaded your sample and replaced your cookie code with
FormsAuthentication.SetAuthCookie(model.UserName, true);
和它的工作预期:与2天配置表单超时,我的cookie将在两天内到期
And it's working as expected: with two days configured as your form timeout, my cookie will expire in two days.
这篇关于什么是的FormsAuthenticationTicket isPersistent财产的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!