ASP.NET身份验证在自定义机票上滑动到期时间

我使用以下代码创建我自己的身份验证票证:
string formsCookieStr = string.Empty;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1,// version
            username,// user name
            DateTime.Now,// issue time
            DateTime.Now.AddMinutes(30),// expires
            false,// Persistence
            userRoleData                    // user data
    );
formsCookieStr = FormsAuthentication.Encrypt(ticket);
HttpCookie FormsCookie = new HttpCookie(FormsAuthentication.FormsCookieName,formsCookieStr);
HttpContext.Response.Cookies.Add(FormsCookie);

我希望有效期是一个滑动过期 – 每次客户端发送一个请求,那么到期应该重置为30分钟.但是,我只在用户首次登录时创建机票.ASP.NET会自动保持滑动到期的时间,还是需要手动执行某些操作来实现滑动到期?

解决方法

这是在web.config的表单部分中配置的.滑动过期的工作方式是在每个请求上,ASP.NET引擎通过增加超时来重写身份验证cookie:
<authentication mode="Forms">
  <forms 
      loginUrl="~/Account/logon" 
      timeout="2880" 
      slidingExpiration="true" 
  />
</authentication>

不过请注意,启用滑动过期是在ASP.NET Security Practices被认为是坏习惯的事情之一.

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...