首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在MVC5中为OwinContext设置TimeOut

如何在MVC5中为OwinContext设置TimeOut
EN

Stack Overflow用户
提问于 2014-04-09 02:01:01
回答 2查看 17.1K关注 0票数 18

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

如何设置超时时间?使用MVC 5。

我的身份验证如下所示:

代码语言:javascript
复制
        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); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-10 07:10:01

设置固定过期时间跨度的方法是在Startup.Auth.cs文件中设置ExpireTimeSpan属性,如下所示:

代码语言:javascript
复制
// 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设置为持久化。在您的代码中,除了用户名和密码之外,还必须传入一个布尔值,然后更改

代码语言:javascript
复制
authenticationManager.SignIn(id); 

成为

代码语言:javascript
复制
authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, id); 
票数 31
EN

Stack Overflow用户

发布于 2016-12-19 12:07:06

使用以下代码,您不需要使用Startup.cs

代码语言:javascript
复制
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddHours(1), }, id);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22944783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档