如何在MVC 5设置超时OwinContext [英] How to set TimeOut for OwinContext in MVC 5

查看:178
本文介绍了如何在MVC 5设置超时OwinContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户访问一个网站,并输入他们的凭据都存储在我们的数据库中,我们什么时候创建一个认证。

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

我的验证是这样的:

  VAR索赔=新的List<权利要求GT;();
        claims.Add(新索赔(用户ID,user.UserID.ToString()));
        claims.Add(新索赔(ClaimTypes.Name,user.FirstName ++ user.LastName));
        claims.Add(新索赔(ClaimTypes.Email,user.Email));
        claims.Add(新索赔(ClaimTypes.NameIdentifier,user.UserID.ToString()));
        VAR ID =新ClaimsIdentity(索赔,DefaultAuthenticationTypes.ApplicationCookie);        VAR CTX = Request.GetOwinContext();
        VAR的AuthenticationManager = ctx.Authentication;
        authenticationManager.SignIn(ID);


解决方案

要设置一个固定的到期时间跨度的方法是将 ExpireTimeSpan 属性在 Startup.Auth.cs 文件是这样的:

  //使应用程序能够使用cookie来存储信息,在用户签订
app.UseCookieAuthentication(新CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LOGINPATH =新PathString(/帐号/登录),
    ExpireTimeSpan = TimeSpan.FromDays(2)
});

请注意,您还必须设置cookie继续存在。在您的code你必须在除了用户名和密码布尔传递,然后更改

  authenticationManager.SignIn(ID);

  authenticationManager.SignIn(新AuthenticationProperties {IsPersistent =}了rememberMe,身份证);

When a user access a website and enters their credentials which are stored in our database, we when create an authentication.

How do you set the timeout? Using MVC 5.

My Authentication looks like this:

        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); 

解决方案

The way to set an fixed expiration time span is to set the ExpireTimeSpan property in your Startup.Auth.cs file like this:

// 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)
});

Note that you'll also have to set the cookie to persist. In your code you'll have to pass in a bool in addition to the username and password, and then change

authenticationManager.SignIn(id); 

to be

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

这篇关于如何在MVC 5设置超时OwinContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆