如何在用户会话到期时重定向到页面?

我目前正在使用ASP.NET 2.0框架的Web应用程序。 当用户会话到期时,我需要重定向到某个页面,比如SessionExpired.aspx。 项目中有很多页面,因此向网站的每个页面添加代码并不是一个好的解决方案。 我有MasterPages,我认为可能有所帮助。

谢谢!

您可以在Session_Start事件中的global.asax中处理此问题。 您可以在那里检查请求中的会话cookie。 如果会话cookie存在,则会话已过期:

public void Session_OnStart() { if (HttpContext.Current.Request.Cookies.Contains("ASP.NET_SessionId") != null) { HttpContext.Current.Response.Redirect("SessionTimeout.aspx") } } 

唉,我还没有找到任何优雅的方法来查找会话cookie的名称。

当用户“登录”时,我通常会在主页面上的Page.Header.Controls集合中添加一个HtmlMeta控件。 将它设置为Refresh to您的SessionExpired.aspx页面,具有适当的超时长度,你很高兴。

如果我理解正确,“Session_End”会在内部触发,并且没有与之关联的HTTP上下文:

http://forums.asp.net/t/1271309.aspx

因此,我认为您不能使用它来重定向用户。 我见过其他人建议在global.ascx文件中使用“Session_OnStart()”事件:

http://forums.asp.net/p/1083259/1606991.aspx

我没有尝试过,但将以下代码放在“global.ascx”中可能对您有用:

 void Session_OnStart() { if (Session.IsNewSession == false ) { } else { Server.Transfer("SessionExpired.aspx", False); } } 

我们使用Forms Authentication并在Page_Load方法中调用此方法

 private bool IsValidSession() { bool isValidSession = true; if (Context.Session != null) { if (Session.IsNewSession) { string cookieHeader = Request.Headers["Cookie"]; if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0)) { isValidSession = false; if (User.Identity.IsAuthenticated) FormsAuthentication.SignOut(); FormsAuthentication.RedirectToLoginPage(); } } } return isValidSession; } 

另一种方法是告诉浏览器在一段时间后重定向自己(通过javascript)……但用户可以随时停用它。

会话过期时无法重定向用户,因为没有浏览器请求重定向:

  • 如果用户在会话超时(默认为20分钟)内访问您的站点,则会话尚未结束,因此您无需重定向它们。
  • 如果用户在会话超时后访问您的网站,则会话已结束。 这意味着它们将处于新会话的上下文中 – Session_OnEnd已经为旧会话触发了,而您将获得新会话的Session_OnStart。

除了客户端function(例如JavaScript计时器等)之外,您需要在Session_OnStart中处理重定向 – 但显然您需要将此与重新访问该站点的人区分开来。 一个选项是在会话开始时设置会话cookie(即没有到期的cookie,以便它只持续到浏览器关闭),然后在Session_OnStart中查找该cookie – 如果它存在则是一个已过期的返回用户会话,如果不是,它是一个新用户。

显然,您仍然可以使用Session_OnEnd来整理服务器端 – 这只是您无法使用的客户端交互。

你在Session对象中放置了应该始终存在的东西吗? 换句话说,如果他们登录,您可能会在会话中添加类似UserID的内容

 Session("UserID") = 1234 

因此,如果是这种情况,那么您可以在检查该值的母版页中向代码隐藏添加一些内容。 像这样的东西:

 Dim UserID As Integer = 0 Integer.TryParse(Session("UserID"), UserID) If UserID = 0 Then Response.Redirect("/sessionExpired.aspx") End If 

添加或更新您的Web.Config文件以包含此类或类似内容:

    

您是否希望在没有用户干预的情况下重定向下一个请求或立即重定向? 如果您希望在没有用户干预的情况下重定向,则可以在主页面上使用ClientScript.RegisterStartupScript注入一些javascript,这些javascript将在会话到期时重定向您的客户端。

  System.Text.StringBuilder sb = new System.Text.StringBuilder(); String timeoutPage = "SessionExpired.aspx"; // your page here int timeoutPeriod = Session.Timeout * 60 * 1000; sb.AppendFormat("setTimeout(\"location.href = {0};\",{1});", timeoutPage, timeoutPeriod); Page.ClientScript.RegisterStartupScript(this.GetType(), "timeourRedirect", sb.ToString(), true); 

代码来自这里

 namespace PAB.WebControls 

{使用系统; 使用System.ComponentModel; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI;

 [DefaultProperty("Text"), ToolboxData("<{0}:SessionTimeoutControl runat=server>")] public class SessionTimeoutControl : Control { private string _redirectUrl; [Bindable(true), Category("Appearance"), DefaultValue("")] public string RedirectUrl { get { return _redirectUrl; } set { _redirectUrl = value; } } public override bool Visible { get { return false; } } public override bool EnableViewState { get { return false; } } protected override void Render(HtmlTextWriter writer) { if (HttpContext.Current == null) writer.Write("[ *** SessionTimeout: " + this.ID + " *** ]"); base.Render(writer); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (this._redirectUrl == null) throw new InvalidOperationException("RedirectUrl Property Not Set."); if (Context.Session != null) { if (Context.Session.IsNewSession) { string sCookieHeader = Page.Request.Headers["Cookie"]; if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0)) { if (Page.Request.IsAuthenticated) { FormsAuthentication.SignOut(); } Page.Response.Redirect(this._redirectUrl); } } } } } 

}