2014-10-30 70 views
0

在MVC應用程序中,我使用AJAX調用了大部分CRUD操作。在AJAX調用時重定向sessiont超時頁面

問題是,當會話超時然後,它無法重定向到會話超時頁面。

下面是沒有AJAX調用時工作正常的相關代碼。

[AttributeUsage(AttributeTargets.Class)] //| AttributeTargets.Method 
public class ControllerLogAndAccessFilter : FilterAttribute, IActionFilter 
{ 
    public void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
      \\check if session is null then redirect to session time out page. 
    } 
} 

對於登錄,我只是檢查databsae,沒有爲memebrship提供商。

 [HttpPost] 
     public JsonResult Login(string username, string password, bool RememberMe) 
    { 
     try 
     { 
     UserDTO accDTO = new UserDTO() 
      { 
       UsernAme = username, 
       Password = DataEncryption.EncryptPassword(password) 
      }; 

      UserDTO AccDTO = _iAccount.UserAuthentication(accDTO); 

      if (AccDTO != null) 
      { 
       Session["UserId"] = 1; 
       Session["userdto_Session"] = AccDTO; 
       // Remember me 
       HttpCookie myCookie = new HttpCookie("appCookie"); 
       //chkRememberMe.Checked; 
       if (RememberMe) 
       { 
        myCookie.Values.Add("username", username); 
        myCookie.Values.Add("password", password); 
        myCookie.Expires = DateTime.Now.AddMinutes(20); 
       } 
       else 
       { 
        myCookie.Values.Add("username", string.Empty); 
        myCookie.Values.Add("password", string.Empty); 
        myCookie.Expires = DateTime.Now.AddMinutes(5); 
       } 
       Response.Cookies.Add(myCookie); 
       // Remember me 

       return Json(AccDTO.SID, JsonRequestBehavior.AllowGet); 
      } 
      else 
      { 
       return Json(null); 
      } 
     } 
     catch (Exception ex) 
     { 

     } 
     return null; 
    } 

Ajax調用用於登錄,所有的CRUD操作使用$.AJAX({...});製成。

+0

顯示了AJAX調用 – ssilas777 2014-10-30 04:46:23

+0

檢查這個職位代碼:HTTP ://sackoverflow.com/questions/26638368/asp-net-mvc-redirect-out-of-a-partial-view-from-controller-to-a-full-view-from-a – 2014-10-30 05:43:10

+0

@EhsanSajjad我正在使用正常的杜松子酒 - 數據庫驗證。所以,[Authorize]屬性不能像上面提到的stackoverflow鏈接那樣處理或工作。 – dsi 2014-10-30 06:03:24

回答

0

Ajax請求應在客戶端本身進行處理,試試這個

屬性:

[AttributeUsage(AttributeTargets.Class)] //| AttributeTargets.Method 
public class ControllerLogAndAccessFilter : FilterAttribute, IActionFilter 
{  
    public void OnActionExecuting(ActionExecutingContext filterContext) 
     { 

      //TO HANDLE AJAX REQUESTS 
      if (filterContext.HttpContext.Request.IsAjaxRequest()) 
      { 
       //If session is null 
       filterContext.Result = new JsonResult 
       { 
        Data = new 
        { 
         // put a message which sentto the client 
         message = "Session Time out" 
        }, 
        JsonRequestBehavior = JsonRequestBehavior.AllowGet 
       }; 
      }else{ 
        // 
      } 
     } 
    } 

在JavaScript

$.ajax(function(){ 
    url:"", 
    success: function (result) {    
       if(result.message == "Session Time out"){ 
       //Session timed out handle it 
       //window.location.href = session timeout url 
       } 
      }, 

}); 
+0

下面的代碼會調用何時調用ajax? '[AttributeUsage(AttributeTargets.Class)] // | AttributeTargets.Method 公共類ControllerLogAndAccessFilter:FilterAttribute,IActionFilter { 公共無效OnActionExecuting(ActionExecutingContext filterContext) {' – dsi 2014-10-30 05:21:38

+0

我沒有得到你正確的,雖然編輯我的回答更加清晰。 – ssilas777 2014-10-30 05:48:04

+0

我只想問,當我們使用AJAX調用調用動作時,儘管它可以調用過濾器屬性'FilterAttribute' - onexecuting方法? – dsi 2014-10-30 06:05:37

相關問題