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. 所以为了更好地澄清: 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net使用DataTable构造Json字符串的方法
- asp.net-mvc – 如何在ASP.NET MVC部分视图中使用
- asp.net-core – 如何将值更新到appsetting.json
- asp.net – 当不匹配路由时,OWIN中的WebApi总是返
- 在ASP.NET中拒绝用户时,’CustomIdentity’上的S
- asp.net-mvc-3 – 在剃刀中使用@variable后有一个
- asp.net 结合mysql存储过程进行分页代码
- 在ASP.NET中托管时找不到端点
- 下载期间的ASP.net内存使用情况
- asp.net-mvc-4 – 如何在ASP.NET MVC 4上使用SPA
热点阅读