2013-04-18 126 views

回答

4

反CSRF通常通過匹配來自餅乾和身體令牌來驗證在非AJAX調用瀏覽器等形式後的請求來完成。

在Ajax調用,建議把令牌在自定義標題。如果你安裝了最新的ASP.NET 2012.2 update。它在MVC項目對話框中有一個spa模板,演示如何在SPA應用程序中防止CSRF。以下是從模板複製的代碼,以便從服務器端驗證標頭令牌。

public class ValidateHttpAntiForgeryTokenAttribute : AuthorizationFilterAttribute 
{ 
    public override void OnAuthorization(HttpActionContext actionContext) 
    { 
     HttpRequestMessage request = actionContext.ControllerContext.Request; 

     try 
     { 
      if (IsAjaxRequest(request)) 
      { 
       ValidateRequestHeader(request); 
      } 
      else 
      { 
       AntiForgery.Validate(); 
      } 
     } 
     catch (HttpAntiForgeryException e) 
     { 
      actionContext.Response = request.CreateErrorResponse(HttpStatusCode.Forbidden, e); 
     } 
    } 

    private bool IsAjaxRequest(HttpRequestMessage request) 
    { 
     IEnumerable<string> xRequestedWithHeaders; 
     if (request.Headers.TryGetValues("X-Requested-With", out xRequestedWithHeaders)) 
     { 
      string headerValue = xRequestedWithHeaders.FirstOrDefault(); 
      if (!String.IsNullOrEmpty(headerValue)) 
      { 
       return String.Equals(headerValue, "XMLHttpRequest", StringComparison.OrdinalIgnoreCase); 
      } 
     } 

     return false; 
    } 

    private void ValidateRequestHeader(HttpRequestMessage request) 
    { 
     string cookieToken = String.Empty; 
     string formToken = String.Empty; 

     IEnumerable<string> tokenHeaders; 
     if (request.Headers.TryGetValues("RequestVerificationToken", out tokenHeaders)) 
     { 
      string tokenValue = tokenHeaders.FirstOrDefault(); 
      if (!String.IsNullOrEmpty(tokenValue)) 
      { 
       string[] tokens = tokenValue.Split(':'); 
       if (tokens.Length == 2) 
       { 
        cookieToken = tokens[0].Trim(); 
        formToken = tokens[1].Trim(); 
       } 
      } 
     } 

     AntiForgery.Validate(cookieToken, formToken); 
    } 
} 

從客戶端,您還需要在ajax調用中設置標頭。這是todo.datacontext.js代碼:

function ajaxRequest(type, url, data, dataType) { // Ajax helper 
    var options = { 
     dataType: dataType || "json", 
     contentType: "application/json", 
     cache: false, 
     type: type, 
     data: data ? data.toJson() : null 
    }; 
    var antiForgeryToken = $("#antiForgeryToken").val(); 
    if (antiForgeryToken) { 
     options.headers = { 
      'RequestVerificationToken': antiForgeryToken 
     } 
    } 
    return $.ajax(url, options); 
} 
0

我寫了一個動作過濾器類來處理反僞造的令牌驗證..

public class WebApiValidateJsonToken : System.Web.Http.Filters.ActionFilterAttribute 
{ 
    public override void OnActionExecuting(HttpActionContext filterContext) 
    { 
     //get the base url or virtual directory of web app 
     var virtualDirectory = filterContext.Request.Headers.GetValues("vd").First(); 

     //base 64 encode the virtual directory 
     var encodedVirtualDirectory = Convert.ToBase64String(Encoding.UTF8.GetBytes(virtualDirectory)); 

     //set cookie name to look for to append '_' + base 64 encoded virtual directory to match how Asp.Net Html.AntiforgeryToken 
     //names the cookie that holds the token 
     var cookieName = "__RequestVerificationToken" + (String.IsNullOrEmpty(encodedVirtualDirectory) ? "" : "_" + ReplacePadding(encodedVirtualDirectory)); 

     //get the formtoken created by the Html.AntiforgeryToken helper (hidden input element in view) value sent in ajax header 
     var formtoken = filterContext.Request.Headers.GetValues("antiForgeryToken").First(); 

     //get the cookie token value with the name generated from above 
     var cookietoken = filterContext.Request.Headers.GetCookies(cookieName).FirstOrDefault()[cookieName].Value; 


     try 
     { 
      //Validate antiforgery token the old way because can't use ValidateAntiforgeryToken attribute with JSON content 
      System.Web.Helpers.AntiForgery.Validate(cookietoken, formtoken); 
      base.OnActionExecuting(filterContext); 
     } 
     catch 
     { 
      throw new ArgumentException("CSRF"); 
     } 


    } 

    //Supporting Method to strip base 64 '=' padding and replace with the number representation of padding 
    public string ReplacePadding(string b64) 
    { 
     var count = b64.Count(x => x == '='); 
     b64 = b64.Substring(0, b64.Length - count); 
     return b64 + count.ToString(); 
    } 
} 

這是我如何實現它在視圖:

baseUrl = '@Url.Content("~/")'; 

     $.ajaxSetup({ 
      headers: { 
       antiForgeryToken: $('input[name="__RequestVerificationToken"]').val(), 
       vd: baseUrl.substring(0, baseUrl.length - 1) 
      } 
     }); 



       $.ajax({ 
        url: '@Url.Content("~/api/WebApiAction")', 
        data: JSON.stringify(paramObject), 
        contentType: "application/json", 
        type: 'PUT' 
       }) 
       .done(function (data) {/*do stuff*/});