码迷,mamicode.com
首页 > Web开发 > 详细

asp.net core 系列之允许跨域访问-1(Enable Cross-Origin Requests:CORS)

时间:2019-05-02 09:48:17      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:structure   configure   example   add   使用   data-   with   ati   set   

 

接上篇的允许跨域

4.CORS 策略(Policy)的选项

这里讲解Policy可以设置的选项:

  • 设置允许的访问源
  • 设置允许的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:

  • 允许任何HTTP method
  • 影响预检请求(preflight request)和 Access-Control-Allow-Methods header

设置允许的请求头(request header)

要允许一个CORS请求中指定的请求头,可以使用 WithHeaders 来指定。

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

允许所有的请求头

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

 

asp.net core 系列之允许跨域访问-1(Enable Cross-Origin Requests:CORS)

标签:structure   configure   example   add   使用   data-   with   ati   set   

原文地址:https://www.cnblogs.com/Vincent-yuan/p/10801513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!