使用MVC5进行身份验证超时

时间:2017-12-28 12:57:35

标签: asp.net session cookies asp.net-mvc-5

我正在使用ASP.NET身份与MVC5,并希望在一段时间后过期登录用户。我在web.config中添加了一个到system.web的部分:

<authentication mode="Forms">
  <forms timeout="1" slidingExpiration="false"/>
</authentication>

我还将登录代码更改为不使用持久性cookie:

var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

但是用户永远不会退出,他们只会永远登录。

1 个答案:

答案 0 :(得分:0)

表单auth和ASP.NET Identity之间似乎有区别。如果您正在使用Identity,则web.config设置不会产生任何影响。

Identity的设置位于App_Start \ Startup.Auth.cs:

app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/"),
        ExpireTimeSpan = TimeSpan.FromMinutes(24),
        SlidingExpiration =false
    });