【问题标题】:ASP.NET MVC Session Timeout doesn't workASP.NET MVC 会话超时不起作用
【发布时间】:2014-12-13 03:02:37
【问题描述】:

如果我将会话超时设置为少于 20 分钟,一切正常。

但如果超过 20 分钟,它就不起作用。它发生在 VS 2013 和生产 IIS 上。

这是我用过的代码。

如何解决这个问题?谢谢!

STARTUP.AUTH

  public partial class Startup
    {  
        public void ConfigureAuth(IAppBuilder app)
        {
            var sessionTimeout = 5;

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                ExpireTimeSpan = TimeSpan.FromMinutes(sessionTimeout),
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
            });
}

GLOBAL.ASAX

 protected void Session_Start(object sender, EventArgs e)
        {

                Session.Timeout = 5;

        }

附注WEB.CONFIG

   <sessionState mode="InProc"  />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" defaultUrl="~/Account/Login" name="MyApp123"   />
    </authentication>

【问题讨论】:

  • 你试过用 ExpireTimeSpan = new TimeSpan(0, 0, 5, 0)
  • 解释什么是“不起作用”。有没有超时?它仍然在 20 分钟超时?根本没有会话?你如何衡量它的超时?仅供参考,身份验证超时不是会话超时,它们是两个不同的东西。
  • @ErikFunkenbusch 看,我通过我的代码设置了 10 小时的超时时间。但它会在 20 分钟后过期。这就是问题所在。
  • 您可以将会话存储在 SQL 数据库中或增加 IIS 的空闲超时。我建议会话数据库是一种更好的方法,因为您不应该真正依赖 InProc,尤其是如果您希望会话持续 10 小时。
  • @ClarkKent - 如果您使用的是 ASP.NET Identity,您应该有 Authentication mode="None",并且您应该在模块中有一个 remove 语句来删除 FormsAuthentication。

标签: asp.net-mvc iis session-timeout


【解决方案1】:

听起来您的 web.config 中仍有 FormsAuthentication 配置。您正在使用 ASP.NET Identity,它与旧的 FormsAuthentication 冲突。

改成这样:

<authentication mode="None"/>

并确保你有这个:

<system.webServer>
    <modules>
        <remove name="FormsAuthentication" />
    </modules>
</system.webServer>

您也可以使用 asp.net 标识生成一个默认 Web 项目,然后查看 web.config,其中将具有相同的条目。请注意,V1 身份模板有一个错字,在 remove 元素中使用了“FormsAuthenticationModule”而不是“FormsAuthentication”。如果您使用的是 v2 或更高版本,他们会修复该错字。

【讨论】:

    猜你喜欢
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多