繁体   English   中英

给定时间之前的会话超时

[英]Session timeout before given time

我正在使用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整个应用程序中使用formAuthentication吗?

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

任何人都可以给我一些想法或解决方案。

提前致谢!

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

这是最近吸引我的陷阱。 有关更多信息,请参见此MSDN文章

尝试在IIS中提高会话超时值。 该默认值是20分钟。 您可以将web.config设置为具有4年的会话超时,但是IIS会话超时将覆盖它。 假设您的用户在您的网站上未处于活动状态...

表单身份验证Cookie的默认时间是30分钟,这使我认为您的配置有问题。 您可以尝试简化配置以进行测试吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM