我正在使用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);
但是用户永远不会退出,他们只会永远登录。
表单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
});