当用户访问网站并输入存储在我们数据库中的凭据时,我们将创建身份验证。
如何设置超时时间?使用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);
发布于 2014-04-10 07:10:01
设置固定过期时间跨度的方法是在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设置为持久化。在您的代码中,除了用户名和密码之外,还必须传入一个布尔值,然后更改
authenticationManager.SignIn(id);
成为
authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, id);
发布于 2016-12-19 12:07:06
使用以下代码,您不需要使用Startup.cs
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddHours(1), }, id);
https://stackoverflow.com/questions/22944783
复制相似问题