2012-08-01 109 views
2

我在我的網站有很多ajax調用是使用mvc3剃鬚刀構建。 問題是,如果在使用ajax進行操作時會話超時後,它不會重定向到登錄頁面。 我正在使用如下所示的屬性來處理會話超時。沒有重定向到登錄頁面時,會話超時與ajax調用

public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     HttpContext ctx = HttpContext.Current; 

     // check if session is supported 
     if(ctx.Request.IsAuthenticated) 
     { 


      if (ctx.Session != null) 
      { 

       // check if a new session id was generated 
       if (ctx.Session.IsNewSession) 
       { 

        // If it says it is a new session, but an existing cookie exists, then it must 
        // have timed out 
        string sessionCookie = ctx.Request.Headers["Cookie"]; 
        if (null != sessionCookie) 
        { 
         FormsAuthentication.SignOut(); 
         const string loginUrl = @"~/Login/Login"; 
         var rr = new RedirectResult(loginUrl); 
         filterContext.Result = rr; 
        } 
       } 
      } 

     } 
     else 
     { 

      ctx.Response.Redirect(@"~/Login/Login"); 
     } 

     base.OnActionExecuting (filterContext); 
    } 
+1

在問你自己之前搜索與你有關的問題。大多數時候你會得到它們。 – bhuvin 2012-08-01 08:10:51

回答

1

你傳遞的結果是發送一個html頁面和一個302到ajax請求作爲響應。還有其他方法可以解決這個問題:

  1. 給定製的響應代碼,並在ajax請求中檢查它,並檢查成功函數中的響應代碼。
  2. 或者發送一個JSON對象包含響應對象,並檢查它在那裏

這可以在ajaxComplete事件這兩種方法來實現,並可能被添加到母版頁/佈局或哪裏有阿賈克斯電話。

由於存在許多其他問題,因此不會粘貼到代碼中。 歡迎來到StackOverflow Vaibhav!

相關問題