我目前正在使用ASP.NET 2.0框架的Web應用程序。當用戶會話到期時,我需要重定向到某個頁面,例如SessionExpired.aspx。項目中有很多頁面,因此向網站的每個頁面添加代碼並不是一個很好的解決方案。儘管我有MasterPages,我認爲可能會有所幫助。如何在用戶會話過期時重定向到頁面?
謝謝!
我目前正在使用ASP.NET 2.0框架的Web應用程序。當用戶會話到期時,我需要重定向到某個頁面,例如SessionExpired.aspx。項目中有很多頁面,因此向網站的每個頁面添加代碼並不是一個很好的解決方案。儘管我有MasterPages,我認爲可能會有所幫助。如何在用戶會話過期時重定向到頁面?
謝謝!
您可以在Global.asax中在在session_start事件處理這個問題。您可以在該請求中檢查會話cookie。如果會話cookie存在,會話已過期:
public void Session_OnStart()
{
if (HttpContext.Current.Request.Cookies.Contains("ASP.NET_SessionId") != null)
{
HttpContext.Current.Response.Redirect("SessionTimeout.aspx")
}
}
唉我還沒有發現查不到會話cookie的名稱的任何優雅的方式。
當用戶「登錄」時,我通常會將HtmlMeta控件添加到母版頁上的Page.Header.Controls集合中。將它設置爲刷新到SessionExpired.aspx頁面,並設置適當的超時時間,這樣你就可以開始了。
另一種方式是告訴瀏覽器在一段時間後重定向自己(通過javascript)......但是這總是可以由用戶停用。
添加或更新您的web.config文件,包括本或類似的東西:
<customErrors defaultRedirect="url" mode="RemoteOnly">
<error statusCode="408" redirect="~/SessionExpired.aspx"/>
</customErrors>
如果我理解正確的,「Session_End中」內部火災和沒有與其相關聯的HTTP上下文:
http://forums.asp.net/t/1271309.aspx
因此,我不認爲你可以使用它來重定向用戶。我見過別人建議使用「的Session_OnStart()」事件在global.ascx文件:
http://forums.asp.net/p/1083259/1606991.aspx
我還沒有嘗試過,但把下面的代碼在「global.ascx」可能會爲工作你:
void Session_OnStart() {
if (Session.IsNewSession == false)
{
}
else
{
Server.Transfer("SessionExpired.aspx", False);
}
}
當會話過期時,因爲沒有瀏覽器請求重定向你不能將用戶重定向:
除了客戶端功能(如JavaScript的定時器等),因此,你需要處理的的Session_OnStart重定向而不是 - 但很明顯,你需要從別人來到現場重新區分這一點。一種選擇是在會話開始時設置一個會話cookie(即一個沒有到期的cookie,以便它只能持續到瀏覽器關閉),然後在Session_OnStart中查找該cookie - 如果存在,它是一個返回的用戶會話,如果不是,它是一個新用戶。
很明顯,您仍然可以使用Session_OnEnd在服務器端進行整理 - 這僅僅是客戶端交互不可用。
你是否在Session對象中放置了一些應該始終存在的東西?換句話說,如果他們登錄,您可將像用戶名會話
Session("UserID") = 1234
所以,如果是這樣的話,那麼你可以在檢查該母版頁添加了一些您的代碼隱藏值。事情是這樣的:
Dim UserID As Integer = 0
Integer.TryParse(Session("UserID"), UserID)
If UserID = 0 Then
Response.Redirect("/sessionExpired.aspx")
End If
我們使用窗體身份驗證和調用此方法在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;
}
您是否在尋找下一個請求重定向或立即重定向,無需用戶干預?如果您希望在沒有用戶干預的情況下進行重定向,那麼您可以在主頁上使用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);
namespace PAB.WebControls
{ ; using System.ComponentModel; using System.Web; using System.Web.Security; using 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);
}
}
}
}
}
}
如果什麼會話過期,因爲任何其他原因,而不是超時?這可能嗎? – 2008-09-26 16:23:53
到期意味着超時。您可以通過編程方式失去會話(Session.Abandon()),或者如果用戶篡改或刪除了會話cookie,但此時會話本身並未*過期*。也許我並不完全瞭解這個問題...... – 2008-09-26 16:41:14
如果用戶在同一頁面上花費大量時間做一些使用ajax而不是標準GET/POST的東西,該怎麼辦?用這種方法他們仍然可能被註銷。 – coalvilledave 2012-01-27 16:14:11