簡體   English   中英

當會話在具有母版頁的網站上過期時,如何自動重定向到登錄頁面

[英]How to auto redirect to login page when session expires on a site with master page

我正在使用母版頁的asp.net webforms項目。 我希望網站在會話結束時自動重定向到login.aspx頁面。 我嘗試了以下鏈接給出的建議。 https://code.msdn.microsoft.com/Auto-redirect-to-login-e1782b2f基本上我已經將javascript從鏈接復制到母版頁的aspx head部分,並在母版頁的頁面加載事件中使用了C#代碼。 但在設置的3分鍾會話超時后,它不會重定向到登錄頁面。 我的web.config文件中有以下條目。

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" 
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
cookieless="false" timeout="3" />

我甚至嘗試通過此鏈接建議的global.ascx頁面,這也不起作用。 http://www.c-sharpcorner.com/UploadFile/0c1bb2/redirect-page-after-session-time-out-in-Asp-Net424/謝謝

您可以在母版頁中檢查會話,如:

protected void Page_Load(object sender, EventArgs e)
  {
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    {
        if (!IsPostBack)
        {
           //User session active
        }
    }
    else
    {
        //User session not active
        Response.Redirect("Login.aspx", false);
    }
}

並更新Web.config,如:

<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx" protection="All" timeout="3000" path="/" />
</authentication>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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