【问题标题】:ASP.Net Session Timeout detection: Is Session.IsNewSession and SessionCookie detection the best way to do this?ASP.Net 会话超时检测:Session.IsNewSession 和 SessionCookie 检测是最好的方法吗?
【发布时间】:2010-09-30 10:44:14
【问题描述】:

当我的 ASP.Net 会话超时(以及表单身份验证)并且我尝试点击一个页面时,我会自动重定向到我的默认 login.aspx 页面。

在页面加载之前,我需要确定这是否是超时情况,如果是 - 重定向到 timeout.aspx。

下面的文章指出,如果 IsNewSession 为真,并且存在 sessionID cookie - 那么你有一个超时情况。

但是在我的测试中,我遇到了超时并尝试重新登录的情况,并且 IsNewSession 等于 true 并且 sessionId cookie 仍然存在(因为它在整个浏览器会话中都存在),因此它说我我刚尝试重新登录时又超时了。

有没有更好的方法来完成这一切?

技术描述为herehere

在我的“global.asax”文件中:

void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
        // Check if session state is enabled in web.config
        if (Context.Session == null) return;

        if (Session["user"] == null) 
        {
            if (Session.IsNewSession)
            {                    
                HttpCookie sessionCookie = Request.Cookies["ASP.NET_SessionId"];
                if ((null != sessionCookie) && !string.IsNullOrEmpty(sessionCookie.Value))
                {
                    /* Session Timeout! */
                    FormsAuthentication.SignOut(); //just in case not done yet
                    Session.Abandon();
                    Response.Redirect("timeout.aspx");
                }
                else
                {
                    // Cookie didn't exist - must be a brand new login
                    return;
                }
            }
            else
            {
                // If there is no session data and the session is not new then it must be the postback of the login screen.
                if ((HttpContext.Current.Request.Path.ToLower().LastIndexOf("/login.aspx") >= 0) && (Request.HttpMethod == "POST"))
                {
                    return;
                }
            }
        }    
}

【问题讨论】:

    标签: asp.net session timeout


    【解决方案1】:

    您正在尝试区分超时会话和手动注销的会话?

    您的问题是,由于会话数据已经消失,您所要做的就是进来的新请求创建了一个新会话,并且进来的请求带有会话 ID cookie(表明它之前已经登录过)。

    有两种方法。

    Cookie:

    首先在您的登录页面中,您可以创建一个额外的 cookie 来指示用户的登录状态。当用户手动注销时,cookie 值被修改以指示注销。会话超时后的请求除了 IsNewSession 为 true 外,还会有一个登录状态 cookie,显示用户仍处于登录状态,从而表明用户没有手动选择注销。

    数据库:

    第二种方法是将 sessionID 与登录状态一起存储在数据库表中。登录成功后,将 sessionID 输入到 LoggedOnSessions 表中。当用户手动注销时,从表中删除 sessionID。因此,您的超时检测可以包括在表中查找会话 ID(如果存在超时)(此时您可能也应该删除该 ID)。

    出于管理目的,您应该包含一个过期日期时间字段,该字段设置的时间比任何实际登录期(例如一周)要长得多。定期(例如每周)删除表中已过期的条目。

    我的偏好是我讨厌设置 cookie 的数据库方法,因为它让我很恼火,每个请求都会发送该 cookie,但很少需要。

    【讨论】:

      【解决方案2】:

      您也可以在标签 authentication 下查看 web.config 文件。这应该看起来像这样:

      <authentication mode="Windows">
        <forms defaultUrl="Default.aspx" loginUrl="Login.aspx" name=".aspxAuth">
        </forms>
      </authentication>
      

      注意属性 mode,它可能在 web.config 文件中显示 Forms 而不是 Window。在这种情况下,如果您丢失会话,然后单击任何链接(例如 SalesChart.aspx),ASP.NET 将直接将您带到 Login.aspx 代码隐藏而不是 SalesChart.aspx 代码隐藏,这特别烦人。

      如果您尝试 Windows 模式,您每次都会被带到请求的页面 (SalesChart.aspx),然后您自己决定如果会话丢失要采取什么措施。

      【讨论】:

        猜你喜欢
        • 2013-04-27
        • 2010-09-05
        • 1970-01-01
        • 2015-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 1970-01-01
        相关资源
        最近更新 更多