【问题标题】:ASP.NET Webforms site using HTTPCookie with 100 year timeout times out after 20 minutes使用 HTTPCookie 的 ASP.NET Webforms 站点 100 年超时 20 分钟后超时
【发布时间】:2010-06-11 20:20:27
【问题描述】:

我有一个使用 Forms Auth 的网站。客户端根本不希望用户的站点会话过期。在登录页面代码隐藏中,使用了以下代码:

// user passed validation
FormsAuthentication.Initialize();

// grab the user's roles out of the database 
String strRole = AssignRoles(UserName.Text);

// creates forms auth ticket with expiration date of 100 years from now and make it persistent
FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1,
  UserName.Text, DateTime.Now,
  DateTime.Now.AddYears(100), true, strRole,
  FormsAuthentication.FormsCookiePath);

// create a cookie and throw the ticket in there, set expiration date to 100 years from now
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, 
  FormsAuthentication.Encrypt(fat)) { Expires = DateTime.Now.AddYears(100) };

// add the cookie to the response queue
Response.Cookies.Add(cookie);

Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false));

web.config 文件的身份验证部分如下所示:

<authentication mode="Forms">
      <forms name="APLOnlineCompliance" loginUrl="~/Login.aspx" defaultUrl="~/Course/CourseViewer.aspx" />
</authentication>

当我登录网站时,我会 see 正确发送 cookie 到浏览器并传回:

HttpFox output http://cid-e79f8e4b07c3e30f.office.live.com/embedphoto.aspx/Public/SessionProblem.png

但是,当我离开 20 分钟左右,回来尝试在网站上做任何事情时,登录窗口会重新出现。这个解决方案在我们的服务器上运行了一段时间 - 现在它又回来了。我的本地开发盒在 VS2008 中运行 Cassini 时不会出现此问题。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: asp.net session-timeout httpcookie


    【解决方案1】:

    会话超时和表单身份验证超时是两个不同的东西。会话超时是否设置为 20 分钟,是否会在 Global.asax 文件中的 Session_End 事件中将您的用户注销?

    【讨论】:

      【解决方案2】:

      默认情况下,IIS 6 中的应用程序池设置为在 20 分钟不活动后关闭。如果您的应用配置中没有任何内容导致您的应用快速关闭,请检查 IIS 管理器中的应用池配置。里面有很多很棒的旋钮可以设置。

      【讨论】:

        【解决方案3】:

        我在 Global.asax 中确实有以下内容:

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
            {
                //Fires upon attempting to authenticate the use
                if (!(HttpContext.Current.User == null))
                {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        if (HttpContext.Current.User.Identity.GetType() == typeof(FormsIdentity))
                        {
                            FormsIdentity fi = (FormsIdentity) HttpContext.Current.User.Identity;
                            FormsAuthenticationTicket fat = fi.Ticket;
        
                            String[] astrRoles = fat.UserData.Split('|');
                            HttpContext.Current.User = new GenericPrincipal(fi, astrRoles);
                        }
                    }
                }
            }
        

        你指的是这个吗?另外,如果这有什么不同的话,我们处于 IIS6 环境中。

        【讨论】:

        • 看起来不错。我的意思是有两个单独的超时 - 一个用于您的身份验证票,设置为 100 年。但是也有一个会话超时,默认设置为 20 分钟,所以在我看来它正在影响某些东西。一种常见的模式是将 Session_End 事件连接到 Global.asax 中的处理程序,该处理程序将在会话到期时将用户从他们的表单验证票中注销。
        • 对 - 我没有那种东西。有没有办法强制会话超时为空或无穷大,使其默认为 HTTPCookie?
        • 会话超时可以在web.config中设置:msdn.microsoft.com/en-us/library/h6bb9cz9.aspx 但是不能设置超过 1 年。
        • 添加 到 web.config - 20 分钟后仍然转储:-(
        【解决方案4】:

        另一个需要快速检查的可能是您的主机类型。云主机通常会有一个负载均衡器,很难设置相同的 IP 指向节点服务器约 20 分钟,但在此之后,您可能会被推送到新服务器,在新服务器上创建新会话并“记录您”出'

        如果您在标准共享主机或单个专用服务器或虚拟服务器上,但这不会是问题:)

        要解决这个问题并保持 asp.net 会话正常工作,您需要将会话状态移动到数据库 - 或重新编写代码以完全不使用会话 :)

        【讨论】:

          【解决方案5】:

          您可能想要检查您是否正在使用负载平衡器。如果是这样,那么你真的不应该存储 InProc。如果您有多个实体,则应查看状态服务器或 sql 服务器。

          根据问题,似乎也没有遵守默认的 30 分钟,这通常指向 IIS/Hosting/Network 配置。

          【讨论】:

            猜你喜欢
            • 2011-02-27
            • 2023-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-29
            • 2016-04-07
            • 2010-12-16
            • 1970-01-01
            相关资源
            最近更新 更多