這篇文章介紹如何容許跨域訪問html
瀏覽器安全不容許不一樣域名的網頁之間發送請求。這種限制叫作同源策略(the same-origin policy)。api
同源策略能夠防止一個惡意的站點讀取另外一個站點的敏感數據。跨域
有時候,你想容許網站發送跨域的請求到你的應用。瀏覽器
Cross Origin Resource Sharing ( CORS ) : 安全
若是兩個URLs是同源的,那麼它們有相同的協議,主機(域名),端口app
下面兩個是同源的URLs:cors
https://example.com/foo.html
https://example.com/bar.html
下面的這些相比於前面的兩個URL,有不一樣的源:dom
https://example.net
– Different domain 不一樣的域名https://www.example.com/foo.html
– Different subdomain 不一樣的子域名http://example.com/foo.html
– Different scheme 不一樣的協議https://example.com:9000/foo.html
– Different port 不一樣的端口號IE瀏覽器考慮同源問題的時候,不會考慮端口號測試
CORS中間件處理跨域請求。下面的代碼容許指定的源能對整個應用進行跨域請求網站
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) {
//AddCors方法的調用會把CORS服務加到應用的服務容器中(service container); services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.WithOrigins("http://example.com", //CorsPolicyBuilder方法能夠鏈式調用方法, "http://www.contoso.com"); }); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseCors(MyAllowSpecificOrigins); //這個代碼會把CORS策略經過CORS中間件應用到這個應用的全部終端(endpoints);即把跨域做用到整個應用
//注意:1.UseCors必須在UseMvc以前被調用;2. URL末尾不能加/ ;這個url指的是 builder.WithOrigins(url)中的url
app.UseHttpsRedirection();
app.UseMvc();
}
}
這段代碼作了下面的操做
CorsPolicyBuilder方法能夠鏈式調用方法:
builder.WithOrigins("http://example.com", "http://www.contoso.com") .AllowAnyHeader() .AllowAnyMethod();
測試跨域
[EnableCors]屬性提供了另外一種方式設置跨域。便可以只設置選擇的終端,而不是全部的終端.
這裏不一樣於上面的那種方式,上面的方式是應用的全部終端都會被設置容許跨域;
而這裏只是設置了[EnableCors]屬性的終端;
使用[EnableCors]來指定默認的策略,而[EnableCors("{Policy String}")] 指定了特定的策略;
[EnableCors]屬性應用於:
你能夠使用[EnableCors]屬性應用不一樣的策略到 controller/page-model/action 中;
當[EnableCors]屬性應用到 controller/page-model/action ,而且CORS在中間件被容許了(指【Enable("{Policy String}")】的方式),這兩種策略就都被使用了;
不推薦結合使用策略;使用[EnableCors]屬性或者中間件,而不是在相同的應用中使用兩個
下面的代碼給每一個方法使用了一種策略
[Route("api/[controller]")] [ApiController] public class WidgetController : ControllerBase { // GET api/values [EnableCors("AnotherPolicy")] [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "green widget", "red widget" }; } // GET api/values/5 [EnableCors] // Default policy. [HttpGet("{id}")] public ActionResult<string> Get(int id) { switch (id) { case 1: return "green widget"; case 2: return "red widget"; default: return NotFound(); } } }
下面的代碼建立了一個跨越默認策略和一個名字叫「AnotherPolicy」的策略:
public class StartupMultiPolicy { public StartupMultiPolicy(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddDefaultPolicy( builder => { builder.WithOrigins("http://example.com", "http://www.contoso.com"); }); options.AddPolicy("AnotherPolicy", builder => { builder.WithOrigins("http://www.contoso.com") .AllowAnyHeader() .AllowAnyMethod(); }); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseMvc(); } }
另,其實還有[DisableCors]屬性能夠禁止CORS,這裏先暫時不作講解
...未完,待續