当用户访问网站并输入存储在我们的数据库中的凭据时,我们在创建身份验证时.
你如何设置超时?
使用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);