当用户访问网站并输入存储在我们的数据库中的凭据时,我们在创建身份验证时.
你如何设置超时?
使用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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。