【发布时间】:2011-03-26 06:57:26
【问题描述】:
替代标题:如何在会话超时时重定向
最终解决方案:感谢:Robin Day(虽然我测试了 Ben 的解决方案,它也有效,其他两个解决方案也都是很好的解决方案)
我摆脱了我最初拥有的基本页面。
把这个放在 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 页面发生超时,此解决方案也有效
结束解决方案
我有一个检查会话超时的基本页面。 (这就是它所做的一切)。如果会话超时,我想重定向到主页。但是,主页也继承自此基本页面。
我不确定我是否解释得很好:
第 1 步:加载我的一个页面
第 2 步:等待超过 20 分钟(这会导致会话超时)。
第 3 步:我点击了导致回邮的内容
第 4 步:Basepage 检测超时并重定向到 default.aspx
第 5 步:加载 default.aspx 时,基本页面检测到仍然存在超时,并再次尝试重定向到 default.aspx。 第 6 步:重复第 5 步
粗体是不想要的效果...
这是基本页面代码。
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");
}
}
}
}
}
谢谢!!!
(如果您需要进一步说明,请询问)
注意:我知道如果我重定向到一个不从该基本页面继承的页面,它将解决问题。但我不喜欢这种解决方案。
【问题讨论】:
标签: asp.net session redirect recursion timeout