asp.net – 如何在MVC 5中为OwinContext设置TimeOut

用户访问网站并输入存储在我们的数据库中的凭据时,我们在创建身份验证时.

你如何设置超时?
使用MVC 5.

我的身份验证如下所示:

var claims = new List<Claim>();
        claims.Add(new Claim("UserId",user.UserID.ToString()));
        claims.Add(new Claim(ClaimTypes.Name,user.FirstName + " " + user.LastName));
        claims.Add(new Claim(ClaimTypes.Email,user.Email));
        claims.Add(new Claim(ClaimTypes.NameIdentifier,user.UserID.ToString()));
        var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);

        var ctx = Request.GetowinContext();
        var authenticationManager = ctx.Authentication;
        authenticationManager.SignIn(id);

解决方法

设置固定的到期时间跨度的方法是在Startup.Auth.cs文件中设置ExpireTimeSpan属性,如下所示:
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationoptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),ExpireTimeSpan = TimeSpan.FromDays(2)
});

请注意,您还必须将cookie设置为持久.在您的代码中,除了用户名和密码之外,还必须传入bool,然后更改

authenticationManager.SignIn(id);

成为

authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe },id);

相关文章

这篇文章主要讲解了“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...