簡體   English   中英

在Session_end事件上重定向到另一個頁面

[英]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。

asp.net:在沒有會話的情況下將代碼重定向到主頁的位置?

我認為你得到“響應在這種情況下不可用”,因為用戶沒有向服務器發出請求,因此你無法為它提供響應。 請嘗試使用Server.Transfer。

我覺得最簡單的方法就是使用Meta信息並使技巧發揮作用。 考慮我們有一個頁面WebPage.aspxWebPage.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM