检查会话是否已超时

时间:2010-08-06 15:24:27

标签: asp.net session timeout

这是除EndSession.aspx之外的所有页面的基类

override protected void OnInit(EventArgs e)  {  
base.OnInit(e);  

if (Context.Session != null)  
     {  
         //check the IsNewSession value, this will tell us if the session has been reset.  
         //IsNewSession will also let us know if the users session has timed out  
         if (Session.IsNewSession)  
         {  
            //now we know it's a new session, so we check to see if a cookie is present  
             string cookie = Request.Headers["Cookie"];  
             //now we determine if there is a cookie does it contains what we're looking for 
             if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0) )//&& !Request.QueryString["timeout"].ToString().Equals("yes"))  
             {  
                 //since it's a new session but a ASP.Net cookie exist we know  
                 //the session has expired so we need to redirect them  

                 Response.Redirect("EndSession.aspx?timeout=yes");  
             }  
         }  
     }  
 } 

但是在EndSession上我尝试导航回来,比如default.aspx,然后上面的代码只是重定向回到EndSession.aspx。

因此,为了更好地澄清: 第1步:转到mypage.aspx 第2步:等待超时 第3步:尝试离开 第4步:重定向到EndSession.aspx 第5步:尝试离开 第6步:GoTo设置4

Setp 6实际上应该能够导航......

(如果需要pelase要求进一步澄清)

有什么想法吗?

感谢!!!

1 个答案:

答案 0 :(得分:4)

我摆脱了原来的基页。

将它放在Global.asax的Session_Start

void Session_Start(object sender, EventArgs e) 
{
    string cookie = Request.Headers["Cookie"];
    // Code that runs when a new session is started
    if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))//&& !Request.QueryString["timeout"].ToString().Equals("yes"))  
    {
        if(Request.QueryString["timeout"] == null || !Request.QueryString["timeout"].ToString().Equals("yes"))
            Response.Redirect("Default.aspx?timeout=yes");
    }
}

将它放在Defualt.aspx页面上:

 if (!IsPostBack)
    {
        if (Request.QueryString["timeout"] != null && Request.QueryString["timeout"].ToString().Equals("yes"))
        {
            Response.Write("<script>" +
               "alert('Your Session has Timedout due to Inactivity');" +
               "location.href='Default.aspx';" +
               "</script>");
        }
    }

即使在Default.aspx页面上发生超时,此解决方案也能正常工作

我使用的解决方案的错误发布在这里:How to stop basepage from recursivly detecting session timeout

相关问题