【问题标题】:Session timeout before given time给定时间之前的会话超时
【发布时间】:2012-11-19 17:32:35
【问题描述】:

我在 Asp.Net MVC 应用程序中使用表单身份验证,如下所示:

代码

public void SignIn(string userName, bool isCookiePersistent)
        {

            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(14),
                createPersistentCookie, string.Empty);

            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, isCookiePersistent);
            if (authTicket.IsPersistent)
            {
                authCookie.Expires = authTicket.Expiration;
            }

            authCookie.Value = FormsAuthentication.Encrypt(authTicket);
            HttpContext.Current.Response.Cookies.Add(authCookie);
        }

public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

问题: 问题是,如果我将表单身份验证超时设置为 4 小时,我的用户在登录半小时后仍然重定向到登录页面。

我已经尝试在 web.config 中包括 SessionSate 或排除 SessionState,但注意到正在发生。问题仍然相同。这是我下面的 web.cofig 代码。

Web.config(无 sessionState 元素)

  <authentication mode="Forms">
      <forms loginUrl="~/LogOn/LogOn" requireSSL="false" timeout="240" defaultUrl="~/Home/Home" name="__appcookie" path="/" slidingExpiration="true" ticketCompatibilityMode="Framework40" protection="All">
      </forms>
    </authentication>

Web.config(带有 sessionState 元素)

<sessionState timeout="240"></sessionState>
 <authentication mode="Forms">
          <forms loginUrl="~/LogOn/LogOn" requireSSL="false" timeout="240" defaultUrl="~/Home/Home" name="__appcookie" path="/" slidingExpiration="true" ticketCompatibilityMode="Framework40" protection="All">
          </forms>
        </authentication>

有人可以告诉我在 web.config 中包含 sessionStatesessionTimeout 真的很重要吗?我不能在整个应用程序中只使用formAuthentication 吗?

无论我是否使用sessionState,即使只使用form authentication,我的用户在登录应用程序后半小时后重定向到登录页面。 (但我已经将 240 分钟设置为 form authentication timeout)。

谁能给我一些想法或解决方案。

提前致谢!

【问题讨论】:

  • 还有空闲时间。您确定重定向是在用户处于活动状态时进行的吗?如果不是,您也必须将空闲时间设置为 4 小时。

标签: c# asp.net asp.net-mvc forms authentication


【解决方案1】:

forms ticketCompatibilityMode="Framework40" 指定票证到期日期存储为 UTC。默认值为Framework20,它指定票证到期日期存储为本地时间。如果您像使用 DateTime.Now 一样手动设置 FormsAuthenticationTicket 到期日期,而您的 ticketCompatibilityMode 为 Framework40,则本地和 UTC 之间存在脱节(DateTime.NowDateTime.UtcNow)。

这是我最近遇到的一个问题。见this MSDN article for more information

【讨论】:

    【解决方案2】:

    尝试在 IIS 中提高会话超时值。其默认值为 20 分钟。您可以将 web.config 设置为 4 年内的会话超时,但 IIS 会话超时将覆盖它。假设您的用户在您的网站上不活跃...

    【讨论】:

      【解决方案3】:

      30 分钟是表单身份验证 cookie 的默认时间,这让我相信您的配置有问题。您可以尝试简化配置仅用于测试吗?

      <authentication mode="Forms">
          <forms loginUrl="~/LogOn/LogOn" timeout="240" protection="All" />
      </authentication>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-30
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多