如何从recursivly检测会话超时停车的BasePage [英] How to stop basepage from recursivly detecting session timeout
问题描述
备选标题:如何在会话超时重定向
Alternate Title: How to redirect on session timeout
我摆脱我最初拥有的BasePage的。
I got rid of the basepage that I had originally.
将这个
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页面上:
Put this on the Defualt.aspx page:
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页面上会出现此解决方案
This solution works even when the timeout occurs on the Default.aspx page
端到端的解决方案
我有检查会话超时基页。 (那它所做的一切)。我想重定向到主页,如果有一个会话超时。然而,主页也继承了这个基地页。
I have a base page that checks for session timeout. (thats all it does). I want to redirect to the home page if there is a session timeout. However, the home page also inherits from this base page.
我不知道如果我解释这口井:
I'm not sure if i'm explaining this well:
第1步:我的一个页面负载
Step 1: One of my pages loads
第2步:它位于超过20分钟(这会导致会话超时)
Step 2: It sits for more than 20 minutes(this would cause session timeout).
第三步:点击我的东西,导致poastback
Step 3: I click on something that causes poastback
第四步:检测的BasePage和超时重定向到Default.aspx的
Step 4: Basepage detects timeout and redirects to default.aspx
第五步:作为Default.aspx的负载,检测的BasePage仍然出现超时,并再次尝试重定向到Default.aspx的。
步骤6:重复步骤5
大胆是不希望影响...
这是的BasePage code。
This is the basepage code.
using System;
using System.Web.UI;
public class SessionCheck : System.Web.UI.Page
{
public SessionCheck() {}
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))
{
//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("Default.aspx?timeout=yes&success=no");
}
}
}
}
}
谢谢!
(如果你需要进一步澄清,请询问)
(If you need further clarification please ask)
注:我知道,如果我重定向到不会从该的BasePage继承一个页面,将解决这个问题。但我不喜欢这样的解决方案。
Note: I know that if I redirect to a page that does not inherit from this basepage it would fix the problem. But i don't like this solution.
推荐答案
你能使用事件的Session_OnStart
在的Global.asax
?
每当一个新的会话开始这一次解雇。你可以做你重定向在那里。
This will be fired once whenever a new session begins. You can do your redirect in there.
我想虽然的关键是,你只,如果你不是已经在它重定向到主页。您可以通过只检查URL Request对象做到这一点。
The key I guess though is that you only redirect to the home page if you're not already on it. You can do this by just checking the URL in the Request object.
这篇关于如何从recursivly检测会话超时停车的BasePage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!