2012-10-19 35 views
32

這裏妥善處理HttpAntiForgeryException的方法是情景:在MVC 4應用

我有一個登錄頁面,當用戶簽署它,它會被重定向到家庭應用程序頁面。然後用戶使用瀏覽器後退按鈕,現在他在登錄頁面上。他試圖再次登錄,但現在拋出一個異常:

HttpAntiForgeryException(0X80004005):所提供的防僞標記是爲那些用戶「」,而當前用戶是「username」的。

我知道這與緩存有關。爲登錄操作使用自定義過濾器NoCache的這臺所有必需的頭我禁用瀏覽器緩存 - 無緩存,無店鋪,必重新驗證等,但

  • 這是不工作在所有瀏覽器
  • 尤其是Safari瀏覽器(在大多數情況下,移動)總是忽略這樣的設置

我會盡量讓黑客和強制safari移動刷新,但這不是我所期待的。

我想知道如果我能:

  • 處理異常而不顯示用戶的任何問題的存在(完全透明的用戶)
  • 防止這個問題,通過更換防僞造令牌的用戶名這將如果我的瀏覽器緩存相關黑客將停止在下一個版本的瀏覽器中工作,則允許用戶再次登錄而不會出現此異常。
  • 我真的不想依賴瀏覽器行爲,因爲每個人的行爲都不一樣。

更新1

做出一些澄清,我知道如何在MVC處理錯誤。問題是這個處理錯誤根本不能解決我的問題。錯誤處理的基本思想是用很好的消息重定向到自定義錯誤頁面。但是我想阻止這種錯誤發生,而不是以用戶可見的方式處理它。通過處理我的意思是趕上使用戶名替換或其他適當的行動,然後繼續登錄

更新2

我已經添加了低於該爲我工作的解決方案。

回答

16

經過一段時間的調查,我想我找到了一些方法來擺脫用戶的這個錯誤。它並不完美,但至少沒有顯示錯誤頁面:

我創建基於HandleErrorAttribute過濾器:

[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", 
     Justification = "This attribute is AllowMultiple = true and users might want to override behavior.")] 
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
    public class LoginAntiforgeryHandleErrorAttribute : FilterAttribute, IExceptionFilter 
    { 
     #region Implemented Interfaces 

     #region IExceptionFilter 

     /// <summary> 
     /// </summary> 
     /// <param name="filterContext"> 
     /// The filter context. 
     /// </param> 
     /// <exception cref="ArgumentNullException"> 
     /// </exception> 
     public virtual void OnException(ExceptionContext filterContext) 
     { 
      if (filterContext == null) 
      { 
       throw new ArgumentNullException("filterContext"); 
      } 

      if (filterContext.IsChildAction) 
      { 
       return; 
      } 

      // If custom errors are disabled, we need to let the normal ASP.NET exception handler 
      // execute so that the user can see useful debugging information. 
      if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) 
      { 
       return; 
      } 

      Exception exception = filterContext.Exception; 

      // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method), 
      // ignore it. 
      if (new HttpException(null, exception).GetHttpCode() != 500) 
      { 
       return; 
      } 

      // check if antiforgery 
      if (!(exception is HttpAntiForgeryException)) 
      { 
       return; 
      } 

      filterContext.Result = new RedirectToRouteResult(
       new RouteValueDictionary 
       { 
        { "action", "Index" }, 
        { "controller", "Home" } 
       }); 

      filterContext.ExceptionHandled = true; 
     } 

     #endregion 

     #endregion 
    } 

然後我應用這個過濾器登錄POST操作:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
[LoginAntiforgeryHandleError] 
public ActionResult Login(Login model, string returnUrl) 
{ 

主要思路此解決方案的目的是將防僞造異常重定向到主索引操作。如果用戶仍然不會未經身份驗證,則會顯示登錄頁面如果用戶已經通過身份驗證,它將顯示索引頁面。

UPDATE 1 此解決方案存在一個潛在的問題。如果有人使用不同的憑據登錄,那麼應該添加額外的登錄運行時 - 註銷以前的用戶並登錄新的用戶。這種情況不會被處理。

+0

這對我很好。我不需要它做任何幻想。只是不要扔黃色的屏幕。 – hal9000

5

你應該能夠通過添加一個動作過濾器來處理你的錯誤。

[HandleError(View="AntiForgeryExceptionView", ExceptionType = typeof(HttpAntiForgeryException))] 

Todo所以確保自定義錯誤在您的web.config中打開。

<customErrors mode="On"/> 

你也可以看看這個blog關於句柄錯誤的更多信息。

編輯由於您使用的是MVC4,而博客是關於MVC3的,您也可以看看MSDN library - HandleErrorAttribute,但該版本不應該真正有所作爲。

+0

是的,但是這一切都講述redirectling(所以處理這個錯誤打破當前動作(執行)我想處理這個錯誤而不會斷裂。行動,所以在這種情況下,處理,替換用戶名,並繼續用正確的數據登錄 – Marcin

+0

我認爲,當你ca n捕獲錯誤,您可以將其重定向到一個視圖(控制器),您可以在其中替換用戶名並使用先前輸入的數據繼續登錄。另見:http://stackoverflow.com/questions/1794936/how-do-i-pass-viewdata-to-a-handleerror-view –

15

如果您只有一個或幾個功能受到影響,創建一個過濾器可能會有點技術性的矯枉過正。一個簡單但不通用的解決方案是簡單地刪除[ValidateAntiForgeryToken]的具體方法,如果用戶登錄檢查後添加手動驗證

if (User.Identity.IsAuthenticated) 
{ 
    return RedirectToAction("Index", "Home"); 
} 
System.Web.Helpers.AntiForgery.Validate(); 
/* proceed with authentication here */ 
+0

謝謝,這似乎很好,仍然提供相同的安全性。 –

+0

這沒有與我一起工作,它給出了完全相同的錯誤,但在這一行:System.Web.Helpers.AntiForgery.Validate(); –

1

一個老問題 - 但我今天就遇到了這個問題,我解決它的方法是通過重定向到註銷的動作,像這樣:

public ActionResult Login(string returnUrl) 
{ 
    if (WebSecurity.IsAuthenticated) 
     return RedirectToAction("LogOff"); 

    ... 
}