gpt4 book ai didi

asp.net-mvc - 如何使用表单例份验证来提供保持登录功能?

转载 作者:行者123 更新时间:2023-12-02 08:38:20 26 4
gpt4 key购买 nike

我正在生成 FormsAuthentication 票证,并将其存储在 cookie 中,我将其与 MVC AuthorizeAttribute 一起使用来提供授权。现在,我已经得到了它,所以如果我选中了“保持登录”复选框,cookie 就不会过期(我只是将 cookie.expires 更改为现在 + 1 年,出于测试目的,当他们'已检查以保持登录状态)。

但是,即使我在创建票证时设置了 cookie 是持久的,但票证在超时期限后仍然停止工作。

这是我创建 AuthTicket 的代码:

var now = DateTime.UtcNow.ToLocalTime();
FormsAuthenticationTicket authTicket = new System.Web.Security.FormsAuthenticationTicket(1, username, now, now.Add(FormsAuthentication.Timeout), rememberMe, username, FormsAuthentication.FormsCookiePath);
string encryptedTicket = System.Web.Security.FormsAuthentication.Encrypt(authTicket);
return encryptedTicket;

这与我设置为 cookie 的 cryptoTicket 相同。有人知道如何在 FormsAuthentication 超时后保留此票证吗?我是否只需要手动设置 FormsAuthentication 超时时间?

最佳答案

cookie 的保留时间与登录保持有效的时间不同。基本上,持久性 cookie 是“浏览器关闭后此 cookie 是否应该保留”,并且与此登录的有效时间无关。例如,您可以使用它来存储用户名,以便用户回来时不必重新输入此信息。

如果 cookie 有效,但 cookie 内的登录已过期,您将需要重新登录。如果您希望登录时间更长,您需要延长 <forms> 的时间。 web.config 中的标记,或者您可以在代码中执行此操作,但如果您想延长/缩短过期时间,则需要重新编译。

这里有一些网站进一步解释了这些概念:

http://software-security.sans.org/blog/2012/04/05/forms-authentication-remember-me-its-hard-not-too

Cookie Confusion with FormsAuthentication.SetAuthCookie() Method

关于asp.net-mvc - 如何使用表单例份验证来提供保持登录功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11834488/

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