加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 检查会话是否已超时

发布时间:2020-12-16 09:58:32 所属栏目:asp.Net 来源:网络整理
导读:这是除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
这是除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要求进一步澄清)

有任何想法吗?

谢谢!!!

解决方法

我摆脱了原来的基页.

把它放在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读