gpt4 book ai didi

asp.net - 控制 FormsAuthentication createPersistentCookie 过期

转载 作者:行者123 更新时间:2023-12-02 20:52:17 25 4
gpt4 key购买 nike

在 ASP.NET MVC2 应用程序中,我们有标准的登录操作...

if (ValidateUser(model.Email, model.Password)
{
FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
...

其中 SetAuthCookie 的第二个参数是 createPersistentCookie ,并具有以下文档:

createPersistentCookie
Type: System.Boolean
true to create a persistent cookie
(one that is saved across browser sessions); otherwise, false.

我们希望持久 cookie 在 2 周后过期(即用户可以在 2 周内返回该网站,并且不需要重新进行身份验证。在那之后,他们将被要求再次登录)。

我们如何设置持久 cookie 的过期时间?

最佳答案

你能不这样做吗?

<system.web>
<authentication mode="Forms">
<forms timeout="20160"/>
</authentication>
</system.web>

超时以分钟为单位。

此超时值与您是否创建持久 cookie 无关。它只是说,如果您不明确终止 cookie (FormsAuthentication.SignOut),它将在给定时间段后自动过期。

换句话说,如果你这样做:

FormsAuthentication.SetAuthCookie(someMembershipName, false);

在以下情况下将导致 cookie 过期:

  • 用户关闭浏览器,或者
  • 已达到超时时间。

相反,如果你这样做:

FormsAuthentication.SetAuthCookie(someMembershipName, true);

将导致 cookie 仅在达到超时时才过期。

HTH

编辑:

取自MSDN :

timeout属性描述如下:

Specifies the time, in integer minutes, after which the cookie expires. If the SlidingExpiration attribute is true, the timeout attribute is a sliding value, expiring at the specified number of minutes after the time that the last request was received. To prevent compromised performance, and to avoid multiple browser warnings for users who have cookie warnings turned on, the cookie is updated when more than half of the specified time has elapsed. This might cause a loss of precision. The default is "30" (30 minutes).

Note Under ASP.NET V1.1 persistent cookies do not time out, regardless of the setting of the timeout attribute. However, as of ASP.NET V2.0, persistent cookies do time out according to the timeout attribute.

换句话说,此过期设置仅处理表单例份验证 cookie。

表单例份验证 cookie 是客户端 cookie,它与您可能拥有的其他服务器端 session (即购物车)无关。

该 session 已过期,设置如下:

<sessionstate 
mode="inproc"
cookieless="false"
timeout="20"

关于asp.net - 控制 FormsAuthentication createPersistentCookie 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3748702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com