2012-12-14 13 views
21

我有我在哪裏實施sessionTimeout像一個ASP.NET應用程序MVC4:如何重定向到登錄頁面時,會話狀態超時在asp.net mvc的完成

<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分鐘),我需要重定向到登錄頁面,但重定向不會發生。

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

+1

請檢查以下鏈接。它可能會幫助你。 [登錄頁面URL] [1] [1]:http://stackoverflow.com/questions/356982/how-to-redirect-to-a-dynamic-login-url-在-ASP淨MVC – RGR

回答

28

一種方法是 在會話過期的情況下,在每個操作中你必須檢查它的會話,如果它是空的,那麼重定向到登錄頁面。

但是,這是非常繁忙的方法 要通過這個來,你需要創建自己的ActionFilterAttribute這將做到這一點,你只需要在每一個動作方法添加此屬性。

這是覆蓋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(); 
} 

這將做你的工作。

+0

基本上我需要創建你在我的每一個控制器的I所定義需要給** ** ActionFilterAttribute和索引方法我需要,得到類和每個操作方法** [SessionExpire] ** 。是這樣 – Jonathan

+1

沒有,你只需要在你的app_start只有一個創建類和屬性添加到操作 – user1006544

+0

ü意味着在頁面的global.asax.cs我需要在Application_Start()函數 – Jonathan

6

有一個通用的解決方案:

比方說你有一個控制器名爲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); 
    } 
21

我發現很簡單的方法來重定向登錄當會話在MVC中結束時。我已經測試過了,而且這個工作沒有問題。

總之,我在1分鐘前在_Layout中捕捉會話結束並重定向。

我嘗試一步一步解釋一切。

如果我們想會話端30分鐘之後,並重定向到loginPage看到這樣的步驟:

  1. 改變這樣的web配置(集31分):

    <system.web> 
        <sessionState timeout="31"></sessionState> 
    </system.web> 
    
  2. 添加此JavaScript的在_Layout(當會話1端1 minute此代碼使重定向之前,它使計數時間用戶最後一個動作上的網站,而不是第一次訪問後)

    <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"); 
    } 
    

我希望這對你是一個非常有用的代碼。

+1

在同一頁上感謝你加入這個,我發現這是 –

+0

不客氣「餵你」 –

+0

可能要[重置'setTimeout'(HTTPS有用:// stackoverflow.com/questions/1472705/resetting-a-settimeout),因爲用戶可以繼續與表單進行交互,並保持會話在後端處於活動狀態 – KyleMit

相關問題