1. 程式人生 > >WebAPI Ajax 跨域請求解決方法(CORS實現)

WebAPI Ajax 跨域請求解決方法(CORS實現)

custom XML header 就會 情況 取數 -o cross serve

概述

ASP.NET Web API 的好用使用過的都知道,沒有復雜的配置文件,一個簡單的ApiController加上需要的Action就能工作。

但是在使用API的時候總會遇到跨域請求的問題,特別各種APP萬花齊放的今天,API的跨域請求是不能避免的。

在默認情況下,為了防止CSRF跨站的偽造攻擊(或者是 javascript的同源策略(Same-Origin Policy)),一個網頁從另外一個域獲取數據時就會收到限制。

有一些方法可以突破這個限制,那就是大家熟知的JSONP, 當然這只是眾多解決方法中一種,由於JSONP只支持GET的請求,如今的復雜業務中已經不能滿足需求。

而CORS(Cross Origin Resource Sharing https://www.w3.org/wiki/CORS)跨域資源共享,是一種新的header規範,可以讓服務器端放松跨域的限制,可以根據header來切換限制或者不限制跨域請求。重要的是它支持所有http請求方式。

問題

XMLHttpRequest 跨域 POST或GET請求 ,請求方式會自動變成OPTIONS的問題。

由於CORS(cross origin resource share)規範的存在,瀏覽器會首先發送一次options嗅探,同時header帶上origin,判斷是否有跨域請求權限,服務器響應access control allow origin的值,供瀏覽器與origin匹配,如果匹配則正式發送post請求,即便是服務器允許程序跨域訪問,若不支持 options 請求,請求也會死掉。

原因

瀏覽器為了安全起見,會Preflighted Request的透明服務器驗證機制支持開發人員使用自定義的頭部、GET或POST之外的方法,以及不同類型的主題內容,也就是會先發送一個 options 請求,問問服務器是否會正確(允許)請求,確保請求發送是安全的。

出現 OPTIONS 的情況一般為:

1、非GET 、POST請求

2、POST請求的content-type不是常規的三個:application/x- www-form-urlencoded(使用 HTTP 的 POST 方法提交的表單)、multipart/form-data(同上,但主要用於表單提交時伴隨文件上傳的場合)、text/plain(純文本) 

3、POST請求的payload為text/html 

4、設置自定義頭部

OPTIONS請求頭部中會包含以下頭部:Origin、Access-Control-Request-Method、Access-Control-Request-Headers,發送這個請求後,服務器可以設置如下頭部與瀏覽器溝通來判斷是否允許這個請求。

Access-Control-Allow-Origin、Access-Control-Allow-Method、Access-Control-Allow-Headers

解決方法

此方法功能強大,可以解決ASP.NET Web API復雜跨域請求,攜帶復雜頭部信息,正文內容和授權驗證信息

方法一

  1 public class CrosHandler:DelegatingHandler
  2 
  3 {
  4 
  5     private const string Origin = "Origin";
  6 
  7     private const string AccessControlRequestMethod = "Access-Control-Request-Method";
  8 
  9     private const string AccessControlRequestHeaders = "Access-Control-Request-Headers";
 10 
 11     private const string AccessControlAllowOrign = "Access-Control-Allow-Origin";
 12 
 13     private const string AccessControlAllowMethods = "Access-Control-Allow-Methods";
 14 
 15     private const string AccessControlAllowHeaders = "Access-Control-Allow-Headers";
 16 
 17     private const string AccessControlAllowCredentials = "Access-Control-Allow-Credentials";
 18 
 19     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 20 
 21     {
 22 
 23         bool isCrosRequest = request.Headers.Contains(Origin);
 24 
 25         bool isPrefilightRequest = request.Method == HttpMethod.Options;
 26 
 27         if (isCrosRequest)
 28 
 29         {
 30 
 31             Task<HttpResponseMessage> taskResult = null;
 32 
 33             if (isPrefilightRequest)
 34 
 35             {
 36 
 37                 taskResult = Task.Factory.StartNew<HttpResponseMessage>(() =>
 38 
 39                 {
 40 
 41                     HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
 42 
 43                     response.Headers.Add(AccessControlAllowOrign,
 44 
 45                         request.Headers.GetValues(Origin).FirstOrDefault());
 46 
 47                     string method = request.Headers.GetValues(AccessControlRequestMethod).FirstOrDefault();
 48 
 49                     if (method != null)
 50 
 51                     {
 52 
 53                         response.Headers.Add(AccessControlAllowMethods, method);
 54 
 55                     }
 56 
 57                     string headers = string.Join(", ", request.Headers.GetValues(AccessControlRequestHeaders));
 58 
 59                     if (!string.IsNullOrWhiteSpace(headers))
 60 
 61                     {
 62 
 63                         response.Headers.Add(AccessControlAllowHeaders, headers);
 64 
 65                     }
 66 
 67                     response.Headers.Add(AccessControlAllowCredentials, "true");
 68 
 69                     return response;
 70 
 71                 }, cancellationToken);
 72 
 73             }
 74 
 75             else
 76 
 77             {
 78 
 79                 taskResult = base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(t =>
 80 
 81                 {
 82 
 83                     var response = t.Result;
 84 
 85                     response.Headers.Add(AccessControlAllowOrign,
 86 
 87                         request.Headers.GetValues(Origin).FirstOrDefault());
 88 
 89                     response.Headers.Add(AccessControlAllowCredentials, "true");
 90 
 91                     return response;
 92 
 93                 });
 94 
 95             }
 96 
 97             return taskResult;
 98 
 99         }
100 
101         return base.SendAsync(request, cancellationToken);
102 
103     }
104 
105 }

使用方式,在Global.asax文件添加

 1 protected void Application_Start()
 2 
 3 {
 4 
 5     IOCConfig.RegisterAll();
 6 
 7     AreaRegistration.RegisterAllAreas();
 8 
 9     WebApiConfig.Register(GlobalConfiguration.Configuration);
10 
11     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
12 
13     RouteConfig.RegisterRoutes(RouteTable.Routes);
14 
15     BundleConfig.RegisterBundles(BundleTable.Bundles);
16 
17     GlobalConfiguration.Configuration.MessageHandlers.Add(new CrosHandler());
18 
19 }

方法二

配置文件中添加如下配置,此方法簡單,應對簡單的跨域請求

<system.webServer>

    <httpProtocol>

      <customHeaders>

        <add name="Access-Control-Allow-Origin" value="*" />

        <add name="Access-Control-Allow-Headers" value="Content-Type" />

        <add name="Access-Control-Allow-Methods" value="GET, POST,OPTIONS" />

      </customHeaders>

    </httpProtocol>

<system.webServer>

原文:簡玄冰.com/jianxuanbing/p/7324929.html

參考文獻:https://code.msdn.microsoft.com/windowsdesktop/Implementing-CORS-support-a677ab5d#content

WebAPI Ajax 跨域請求解決方法(CORS實現)