[英]Redirecting to another page on Session_end event
我希望在会话超时时自动重定向到登录页面。
在web.config文件中,我有以下代码
<configuration>
<system.web>
<sessionState mode="InProc" timeout="1"/>
</system.web>
</configuration>
在Global.asax文件中 -
protected void Session_End(object sender, EventArgs e)
{
Response.Redirect("LoginPage.aspx");
}
但超时后,我收到以下错误:
HttpException未被用户代码处理。
在这种情况下无法获得响应。
有什么线索可以解决这个问题吗?
会话结束时调用Session_End - 通常在最后一次请求后20分钟(例如,如果浏览器处于非活动状态或关闭状态)。
由于没有请求,也没有回应。
如果没有活动会话,我建议在Application_AcquireRequestState
进行重定向。 请记住通过检查当前URL来避免循环。
编辑:我不喜欢.Nets内置身份验证,示例在Global.asax
:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
try
{
string lcReqPath = Request.Path.ToLower();
// Session is not stable in AcquireRequestState - Use Current.Session instead.
System.Web.SessionState.HttpSessionState curSession = HttpContext.Current.Session;
// If we do not have a OK Logon (remember Session["LogonOK"] = null; on logout, and set to true on logon.)
// and we are not already on loginpage, redirect.
// note: on missing pages curSession is null, Test this without 'curSession == null || ' and catch exception.
if (lcReqPath != "/loginpage.aspx" &&
(curSession == null || curSession["LogonOK"] == null))
{
// Redirect nicely
Context.Server.ClearError();
Context.Response.AddHeader("Location", "/LoginPage.aspx");
Context.Response.TrySkipIisCustomErrors = true;
Context.Response.StatusCode = (int) System.Net.HttpStatusCode.Redirect;
// End now end the current request so we dont leak.
Context.Response.Output.Close();
Context.Response.End();
return;
}
}
catch (Exception)
{
// todo: handle exceptions nicely!
}
}
如果您使用FormsAuthentication
东西来维护应用程序的安全性,那么这部分(您尝试做的那部分)将为您完成。 如果FormsAuthentication发现用户的会话已过期,则会将他或她重定向回您的登录页面。
其次,不要过多依赖Session_End
因为如果将会话提供程序从InProc更改为SQLServer或其他进程外提供程序,它将永远不会触发 。
您可以使用会话属性IsNewSession
来检测它是否是会话超时。
ASP.NET HttpSessionState
类具有IsNewSession()
方法,如果为此请求创建了新会话,则该方法返回true
。 检测会话超时的关键是在请求中查找ASP.NET_SessionId
cookie。
当然我也同意我们应该将下面的代码放在一些所谓的自定义BasePage中,所有的页面都使用它来有效地实现它。
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
if (Context.Session != null)
{
if (Session.IsNewSession)
{
string CookieHeader = Request.Headers["Cookie"];
if((CookieHeader!=null) && (CookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
{
// redirect to any page you need to
Response.Redirect("sessionTimeout.aspx");
}
}
}
}
如果要将上述代码放在基页类中,请查看此链接以获取更多说明。
您应该使用Application_AcquireRequestState您将发现Application_AuthenticateRequest不再具有会话上下文(或从不具有会话上下文)。 在我对此的研究中,我对这篇文章进行了解决,并为我解决了这个问题。 谢谢你去Waqas Raja。
我认为你得到“响应在这种情况下不可用”,因为用户没有向服务器发出请求,因此你无法为它提供响应。 请尝试使用Server.Transfer。
我觉得最简单的方法就是使用Meta信息并使技巧发挥作用。 考虑我们有一个页面WebPage.aspx
在WebPage.aspx.cs
文件中添加以下代码。
private void Page_Load(object sender, System.EventArgs e){
Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 60) + 5));
if(Session[“IsUserValid”].ToString()==””)
Server.Transfer(“Relogin.aspx”);
}
在上面的代码中,一旦Session过期, WebPage.aspx
会在5秒后刷新。 并且在页面加载中会话被验证,因为会话不再有效。 该页面将重定向到“重新登录”页面。 每次回发到服务器都会刷新会话,同样会在WebPage.aspx
的元信息中WebPage.aspx
。
您只需在web.config中执行以下操作即可
<configuration>
<system.web>
<sessionState mode="InProc" timeout="1" loginurl="destinationurl"/>
</system.web>
</configuration>
因为,我们无法从Session_End重定向,因为那里没有响应/重定向。通过使用它,当会话超时时,您将被重定向到destinationurl。希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.