这篇文章介绍如何容许跨域访问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,这里先暂时不作讲解
...未完,待续