有人可以向我解释ASP.NET Forms身份验证的工作原理,因为我似乎没有得到它并且我一直在退出.
目前,我有用户名,密码和"保持登录状态"复选框.从这些值我创建一个票证和cookie,如下所示:
// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,email,DateTime.UtcNow,DateTime.UtcNow.AddMinutes(30),remember,String.Empty);
// Encrypt ticket
string cookie_contents = FormsAuthentication.Encrypt(ticket);
// Create cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,cookie_contents);
if (remember) {
cookie.Expires = DateTime.UtcNow.AddDays(90);
}
cookie.Path = FormsAuthentication.FormsCookiePath;
cookie.Secure = true;
// Add cookie to response
Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)
我希望通过此代码我可以登录我的网站,并假设我检查"保持登录状态",我保持登录状态至少90天?
然而,我所看到的是,我是在首次登录后至少30分钟退出(这是留给机票的时间?).
Cookie到期和故障单到期之间有什么区别,我如何保持自己的签名.我是否需要为cookie和票证设置90天?
fae*_*ter 22
如果可以避免,请不要直接操纵cookie.您可以FormsAuthentication.SetAuthCookie(username, persistent)用来签署用户.持久在这里意味着"不要使用会话cookie".
然后,您应该在web.config中指定cookie到期时间
<system.web>
<authentication mode="Forms">
<forms timeout="50000000" slidingExpiration="true"/>
</authentication>
</system.web>
Run Code Online (Sandbox Code Playgroud)
滑动到期意味着将为每个请求续订cookie.超时是几分钟,所以这个例子非常高:)
看一下这个问题以及Scott Gu博客的链接:Forms Authentication Cookie Expiration
| 归档时间: |
|
| 查看次数: |
13590 次 |
| 最近记录: |