簡體   English   中英

在ASP.NET MVC中完成會話狀態超時后,如何重定向到登錄頁

[英]How to redirect to logon page when session State time out is completed in asp.net mvc

我有一個ASP.NET MVC4應用程序,其中正在實現sessionTimeout,例如:

<configuration>
  <system.web>
    <sessionState timeout="2"></sessionState>
  </system.web>
</configuration>

並在身份驗證中:

<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="1" />
    </authentication>
  </system.web>
</configuration>

會話過期(2分鍾)后,我需要重定向到登錄頁面,但是不會發生重定向。

如何更改代碼,以便重定向?

一種方法是,在Session Expire的情況下,您必須在每個操作中檢查其會話,如果它為null,則重定向到Login頁面。

但這是非常忙碌的方法,要解決此問題,您需要創建自己的ActionFilterAttribute來執行此操作,只需在每個action方法中添加此屬性即可。

這是重寫ActionFilterAttribute的類。

public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;

            // check if session is supported
            CurrentCustomer objCurrentCustomer = new CurrentCustomer();
            objCurrentCustomer = ((CurrentCustomer)SessionStore.GetSessionValue(SessionStore.Customer));
            if (objCurrentCustomer == null)
            {
                // check if a new session id was generated
                filterContext.Result = new RedirectResult("~/Users/Login");
                return;
            }

            base.OnActionExecuting(filterContext);
        }
    }

然后在實際操作中添加該屬性,如下所示:

[SessionExpire]
public ActionResult Index()
{
     return Index();
}

這將為您工作。

當會話在MVC中結束時,我發現了一種重定向登錄頁面的非常簡單的方法。 我已經對其進行了測試,並且可以正常工作。

簡而言之,我在_Layout的1分鍾前捕獲了會話結束並進行重定向。

我試圖逐步解釋一切。

如果我們要在30分鍾后結束會話並重定向到loginPage,請參見以下步驟:

  1. 像這樣更改網絡配置(設置31分鍾):

      <system.web> <sessionState timeout="31"></sessionState> </system.web> 
  2. _Layout添加此JavaScript(當會話在此代碼進行重定向之前1分鍾結束時,它使用戶進行最后一次操作(而不是首次訪問網站)之后的計數時間)

     <script> //session end var sessionTimeoutWarning = @Session.Timeout- 1; var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000; setTimeout('SessionEnd()', sTimeout); function SessionEnd() { window.location = "/Account/LogOff"; } </script> 
  3. 這是我的注銷操作,它僅使注銷並重定向LoginIn頁面

     public ActionResult LogOff() { Session["User"] = null; //it's my session variable Session.Clear(); Session.Abandon(); FormsAuthentication.SignOut(); //you write this when you use FormsAuthentication return RedirectToAction("Login", "Account"); } 

我希望這對您來說是非常有用的代碼。

有一個通用的解決方案:

假設您有一個名為Admin的控制器,在其中放置了授權用戶的內容。

然后,您可以Initialize OnAuthorization方法重寫Admin控制器InitializeOnAuthorization方法,並在會話超時時將重定向寫入登錄頁面邏輯重定向:

protected override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
    {
        //lets say you set session value to a positive integer
        AdminLoginType = Convert.ToInt32(filterContext.HttpContext.Session["AdminLoginType"]);
        if (AdminLoginType == 0)
        {
            filterContext.HttpContext.Response.Redirect("~/login");
        }

        base.OnAuthorization(filterContext);
    }

暫無
暫無

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

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