2010-08-06 86 views
4

这是我除了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步:设置转到4

SETP 6应该是真正能够浏览了......

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

任何想法?

谢谢!!!

+0

你实际上在努力达到什么,你无法做到,让我们说FormsAuthentication?我的意思是你将重定向到default.aspx的原因是什么,应该发生什么魔术? – Jeroen 2010-08-06 15:33:00

+0

我接受其他想法。虽然我应该提到我并不担心对用户进行身份验证。还有其他事情存储在会话状态中,如果他们未被特意删除可能会导致问题。我愿意接受更好的替代品,但我仍然认为我正在努力做的事情应该是可以实现的。不是吗? – kralco626 2010-08-06 17:46:32

回答

4

我摆脱了我原来的基础页面。

把这个Global.asax中

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"); 
    } 
} 

的在session_start把这个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