我目前正在开发一个使用ASP.NET 2.0框架的Web应用程序。当用户会话到期时,我需要重定向到某个页面,比如SessionExpired.aspx。项目中有很多页面,因此向网站的每个页面添加代码并不是一个好的解决方案。我有MasterPages,我觉得这可能会有所帮助。
谢谢!
答案 0 :(得分:5)
当用户“登录”时,我通常会在主页面上的Page.Header.Controls集合中添加一个HtmlMeta控件。将它设置为Refresh to您的SessionExpired.aspx页面,并具有适当的超时长度,你就可以了。
答案 1 :(得分:5)
您可以在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的名称。
答案 2 :(得分:4)
如果我理解正确,“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);
}
}
答案 3 :(得分:3)
我们使用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;
}
答案 4 :(得分:2)
另一种方法是告诉浏览器在一段时间后重定向自己(通过javascript)......但用户可以随时停用它。
答案 5 :(得分:1)
当会话过期时,您无法重定向用户,因为没有浏览器请求重定向:
除了客户端功能(例如JavaScript计时器等)之外,您需要在Session_OnStart中处理重定向 - 但显然您需要将其与重新访问该站点的人区分开来。一个选项是在会话开始时设置会话cookie(即没有到期的cookie,以便它只持续到浏览器关闭),然后在Session_OnStart中查找该cookie - 如果它存在则是一个已过期的返回用户会话,如果不是,那就是新用户。
显然,您仍然可以使用Session_OnEnd来整理服务器端 - 这只是您无法使用的客户端交互。
答案 6 :(得分:1)
你是否在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
答案 7 :(得分:1)
您还可以查看以下链接中提供的解决方案
Detecting Session Timeout And Redirect To Login Page In ASP.NET
答案 8 :(得分:0)
添加或更新您的Web.Config文件以包含此类或类似内容:
<customErrors defaultRedirect="url" mode="RemoteOnly">
<error statusCode="408" redirect="~/SessionExpired.aspx"/>
</customErrors>
答案 9 :(得分:0)
您是否希望在没有用户干预的情况下重定向下一个请求或立即重定向?如果您希望在没有用户干预的情况下重定向,则可以在主页面上使用ClientScript.RegisterStartupScript注入一些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);
答案 10 :(得分:0)
namespace PAB.WebControls
{ 使用系统; 使用System.ComponentModel; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI;
[DefaultProperty("Text"),
ToolboxData("<{0}:SessionTimeoutControl runat=server></{0}:SessionTimeoutControl>")]
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);
}
}
}
}
}
}