2013-08-16 38 views
0

我有一個ASP.NET MVC 4應用程序,我在會話超時過濾器處理程序中捕獲會話超時以處理會話超時。我也想處理Ajax請求的會話超時。我最初實施了我在question here中找到的代碼。防止在會話超時時打開jQuery模式彈出窗口MVC4

其中最初爲autocompletes和其他ajax調用工作,但唯一的問題是爲ajax調用模態彈出窗口!

所以,現在我已經改變了我的會話超時處理程序看起來像這樣:

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    if (filterContext.HttpContext.Session != null) 
    { 
     if (filterContext.HttpContext.Session.IsNewSession) 
     { 
      var sessionStateDetails =(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState"); 
      var sessionCookie = filterContext.HttpContext.Request.Headers["Cookie"]; 

      if ((sessionCookie != null) && (sessionCookie.IndexOf(sessionStateDetails.CookieName) >= 0)) 
      { 
       if (filterContext.HttpContext.Request.IsAjaxRequest()) 
       { 
        filterContext.HttpContext.Response.Clear(); 
        filterContext.HttpContext.Response.StatusCode = 500; 

即設置狀態代碼爲我500(401沒有工作,因爲我使用的是Windows身份驗證和模擬,所以如果我改變狀態爲401,我得到一個安全彈出的密碼和用戶名)。

然後我把這個陷阱放在.ajaxSetup或.ajaxError中我已經嘗試過了......並且重新指向會話超時動作(根據需要)以顯示會話超時的視圖。

$(document).ajaxError(function (xhr, props) { 
    if (props.status === 500) { 
     ////session redirect goes here 
     var pathArray = window.location.pathname.split('/'); 
     var segment_1 = pathArray[1]; 
     var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout"; 
     window.location = newURL; 
    } 
}); 

問題是在重定向之前,ajax彈出窗口仍然短暫打開。所以它看起來不錯。任何想法,我可以如何防止它打開並重新順利導向?

這裏是我的jQuery的對話框代碼:

$(document).ready(function() { 
    $(".openDialog").live("click", function (e) { 
     e.preventDefault(); 
     $("<div></div>") 
      .addClass("dialog") 
      .attr("id", $(this) 
      .attr("data-dialog-id")) 
      .appendTo("body") 
      .dialog({ 
       title: $(this).attr("data-dialog-title"), 
       close: function() { $(this).remove() }, 
       width: 600, 
       modal: true, 
       height: 'auto', 
       show: 'fade', 
       hide: 'fade', 
       resizable: 'false' 
      }) 
     .load(this.href); 

    }); 

    $(".close").live("click", function (e) { 
     e.preventDefault(); 
     $(this).closest(".dialog").dialog("close"); 
    }); 
}); 

任何幫助,將不勝感激,我想我幾乎沒有與此有關。

+0

我有一個解決方案,但它有點不同:如果你打電話ajax請求,你的會話結束,我打開一個登錄對話框,否則,如果它是正常的請求我重定向到登錄頁面。你感興趣嗎 ? –

+0

如果您不想打開對話框,請不要調用代碼來打開對話框,直到您找到所需的ajax響應。 – rossisdead

+0

嗨rossisdead,我該怎麼做?我現在添加了我的jquery模態代碼。如何在運行之前檢查此問題?對不起,我的jquery不是很好。 – Alicia

回答

2

我設法讓這個工作到最後,如果其他人有同樣的問題,這就是我所做的......我一直使用會話處理程序,但我現在提出一個403而不是500,作爲養了500顯然是錯誤的,有一些不良的副作用:從

   if (filterContext.HttpContext.Request.IsAjaxRequest()) 
       { 
        filterContext.HttpContext.Response.Clear(); 
        filterContext.HttpContext.Response.StatusCode = 403; 

       } 
       else ...usual code goes here .... 

代碼,然後我捕獲Ajax錯誤如下:

$(document).ajaxError(function (xhr, props) { 
    if (props.status == 403) { 
     ////session redirect goes here 
     var pathArray = window.location.pathname.split('/'); 
     var segment_1 = pathArray[1]; 
     var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout"; 
     window.location = newURL; 
    } 
}); 

當然我珀普中p仍然試圖在重新引導之前加載最短的時刻,但我的用戶似乎並不介意。

0

刪除您覆蓋public override void OnActionExecuting(ActionExecutingContext filterContext)和使用(Global.asax中):

protected void Application_EndRequest(object sender, EventArgs args) 
    { 

     HttpContextWrapper context = new HttpContextWrapper(Context); 
     if (context.Response.StatusCode == 302 && context.Request.IsAjaxRequest()) 
      context.Response.RedirectLocation = string.Empty; 
     //MiniProfiler.Stop(); 
    } 

,並在你的JavaScript:

$.ajaxSetup({ //jQuery.ajaxSetup({ 
    statusCode: { 
     302: function() { 
      __userLoginEvent = true; 
     } 
    } 
}); 

$(document).ajaxStop(function() { 
    if (__userLoginEvent) { 
     //your redirection here 

     __userLoginEvent = false; 
    } 
}) 

我用ajaxStop,因爲如果你有多個Ajax調用你想要一個重定向。這對我來說非常合適。

+0

謝謝Olivier,我會試試這個,讓你知道! – Alicia

+0

謝謝奧利弗,這個答案看起來不錯,但它沒有爲我工作,我結束了與我的會話超時處理程序在最後... – Alicia

相關問題