asp.net-mvc - Session.SessionTimeout 在 ASP.NET Identity 中是否已弃用

标签 asp.net-mvc asp.net-mvc-5 asp.net-identity session-timeout global-asax

下面的代码似乎不再在 ASP.NET Identity 中工作? 这是正确的吗?

Global.asax

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

此代码仅定义 session 超时。

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

最佳答案

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

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    ExpireTimeSpan = TimeSpan.FromSeconds(30),
    // other stuff
});            

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

如果您需要在 cookie 过期时自动重新加载页面,则需要在浏览器中使用一些 JavaScript 来检测 session 何时即将过期。

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

关于asp.net-mvc - Session.SessionTimeout 在 ASP.NET Identity 中是否已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26465753/

相关文章:

asp.net-mvc - 即使将其设置为“不”,Telerik MVC 控件仍会加载 jQuery 验证

.net - 如何在ASP.NET MVC应用程序中的线程上放置代码?

jquery - JQM 弹出窗口内的提交按钮未点击 MVC 中的操作方法

asp.net-mvc - 托管 mvc 5 网站时出现安全异常

javascript - MVC - 根据下拉列表显示或隐藏 (div) 字段

c# - User.Identity.GetUserId() 可以被用户伪造吗?

asp.net-mvc - 页面加载时显示 MVC-5 Razor 验证错误

c# - 带有查询字符串的 RouteUrl - 带有重复键

c# - ASP.Net MVC 如何在 ApplicationUser 和我自己的类之间创建一对多关系?

asp.net - 尝试创建角色时,列名区分符无效