是否在ASP.NET标识中不推荐使用Session.SessionTimeout

Dev*_*per 2 asp.net-mvc session-timeout global-asax asp.net-mvc-5 asp.net-identity

看起来以下代码在ASP.NET身份中不再起作用了?它是否正确?

Global.asax中

protected void Session_Start(object sender, EventArgs e)
{
    Session.Timeout = 5; // It has no impact to Session
}
Run Code Online (Sandbox Code Playgroud)

此代码仅定义会话超时.

STARTUP.AUTH.CS

public void ConfigureAuth(IAppBuilder app)
{            
    var sessionTimeout = 20; // 

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        ExpireTimeSpan = TimeSpan.FromMinutes(sessionTimeout),
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        CookieName = ".MyApp1_Authentication",
        SlidingExpiration = true
    });
}
Run Code Online (Sandbox Code Playgroud)

tra*_*max 5

Cookie ExpireTimeSpan定义身份验证cookie的生命周期.

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    ExpireTimeSpan = TimeSpan.FromSeconds(30),
    // other stuff
});            
Run Code Online (Sandbox Code Playgroud)

这将使身份验证cookie在30秒内无效.但它不会为用户重新加载页面,它会仅在下一个请求时将用户重定向到登录页面.

如果您需要在Cookie过期时自动重新加载页面,您需要在浏览器中使用一些JavaScript来检测会话何时到期.

不是真的答案,因为你已经有了问题.只是一个扩展的评论 - )