asp.net core 系列之容許跨域訪問-1(Enable Cross-Origin Requests:CORS)

 

接上篇的容許跨域ajax

4.CORS 策略(Policy)的選項

這裏講解Policy能夠設置的選項:api

  • 設置容許的訪問源
  • 設置容許的HTTP methods
  • 設置容許的請求頭(request header)
  • 設置暴露的響應頭(response header)
  • 跨不一樣源請求的證書(Credentials)
  • 設置過時時間

AddPolicy 在StartUp.ConfigureServices方法中調用;對於一些選項,先閱讀一下,CORS是怎麼工做的,可能會有幫助跨域

 

設置容許的源(Origins)

AllowAnyOrigin  :容許CORS請求從任何源來訪問,這是不安全的瀏覽器

 

注意:指定AllowAnyOrigin 和AllowCredentials 是一種不安全的配置,可能會致使 跨站請求僞造(cross-site request forgery:CSRF)。緩存

當應用使用這兩個配置時,CORS服務返回一個無效的CORS響應。安全

 

SetIsOriginAllowedToAllowWildcardSubdomains:設置策略的 IsOriginAllowed 屬性,使能夠匹配一個配置的帶通配符的域名服務器

options.AddPolicy("AllowSubdomain",
    builder =>
    {
        builder.SetIsOriginAllowedToAllowWildcardSubdomains();
    });

 

設置容許的HTTP methods

AllowAnyMethod:cookie

  • 容許任何HTTP method
  • 影響預檢請求(preflight request)和 Access-Control-Allow-Methods header

設置容許的請求頭(request header)

要容許一個CORS請求中指定的請求頭,能夠使用 WithHeaders 來指定。app

options.AddPolicy("AllowHeaders",
    builder =>
    {
        builder.WithOrigins("http://example.com")
               .WithHeaders(HeaderNames.ContentType, "x-custom-header");
    });

容許全部的請求頭cors

options.AddPolicy("AllowAllHeaders",
    builder =>
    {
        builder.WithOrigins("http://example.com")
               .AllowAnyHeader();
    });

 

一個CORS中間件策略用 WithHeaders匹配特定的頭,而請求中的頭部(記錄在Access-Control-Request-Headers)須要精確匹配WithHeader中的頭部才能夠跨域。

例如:

應用中策略

app.UseCors(policy => policy.WithHeaders(HeaderNames.CacheControl));

請求中的部分數據:

Access-Control-Request-Headers: Cache-Control, Content-Language

CORS中間件會拒絕這個請求,由於Content-Language(HeaderNames.ContentLanguage)沒有在WithHeaders中列出來;

 

設置暴露的響應頭

默認狀況下,瀏覽器不會暴露全部的響應頭給應用。

默承認用的響應頭是:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

 

若是想讓其餘的頭部對應用可用,能夠調用 WithExposedHeaders:

options.AddPolicy("ExposeResponseHeaders",
    builder =>
    {
        builder.WithOrigins("http://example.com")
               .WithExposedHeaders("x-custom-header");
    });

 

跨源(cross-origin)請求中的證書(Credentials)

默認狀況下,瀏覽器不容許在跨域請求中發送證書。

證書中包含緩存(cookies)和HTTP驗證協議(HTTP authentication schemes)。

要再跨域中發送證書,客戶端(瀏覽器)必須設置 XMLHttpRequest.withCredentials 爲 true。

 

直接使用 XMLHttpRequest 

var xhr = new XMLHttpRequest();
xhr.open('get', 'https://www.example.com/api/test');
xhr.withCredentials = true;

使用JQuery

$.ajax({
  type: 'get',
  url: 'https://www.example.com/api/test',
  xhrFields: {
    withCredentials: true
  }
});

使用 Fetch API

fetch('https://www.example.com/api/test', {
    credentials: 'include'
});

 

服務端也須要容許證書。使用AllowCredentials 

options.AddPolicy("AllowCredentials",
    builder =>
    {
        builder.WithOrigins("http://example.com")
               .AllowCredentials();
    });

 

包含 Access-Control-Allow-Credentials 頭部的HTTP 響應(HTTP Response) 將告訴瀏覽器:服務器容許跨域請求的證書;

若是瀏覽器發送證書,可是響應沒有包含一個有效的 Access-Control-Allow-Credentials 頭部 , 瀏覽器不會暴露響應給應用,跨域失敗;

容許跨域證書是一個安全風險。

在跨域中,若是 Access-Control-Allow-Credentials 頭部出現了,則意味着 設置爲全部的源 (setting origin to " * ")會失效。

 

 

參考網址

https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.2#cors-policy-options

相關文章
相關標籤/搜索