跨域(CORS)

1. 什么是跨域

浏览器阻止Web页面向另一个域中发送请求的一种安全策略,此限制称为同源策略,可防止恶意站点读取另一个站点的铭感数据。
跨资源共享(CORS),允许服务器放松同源策略,使用CORS,服务器可以显示的允许某些跨域请求而拒绝其他的跨域请求。
同源策略不会阻止浏览器发送请求。 相反,它会阻止应用程序查看响应。

1.1 什么是同源

协议、IP、端口相同即为同源
同源:

  1. http://example.com/foo.html
  2. http://example.com/bar.html

非同源:

  1. http://example.net-不同域
  2. http://example.com:9000/foo.html-不同端口
  3. https://example.com/foo.html-不同协议
  4. http://www.example.com/foo.html-不同子域

2. 启用CORS

1.包安装

Install-Package Microsoft.AspNet.WebApi.Cors
使用 -Version 标志来面向特定版本。 CORS 包需要 Web API 2.0 或更高版本。
打开文件应用_Start/webapiconfig.cs。 将以下代码添加到webapiconfig.cs方法:

using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
接下来,将 [EnableCors] 特性添加到控制器类:

```csharp
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebService.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
}

origins:*表示允许所有的客服端请求。
origins:http://mywebclient.azurewebsites.net 对于 "源 " 参数, 请使用你在其中部署了 WebClient 应用程序的 URI。 这允许来自 WebClient 的跨源请求,同时仍不允许所有其他跨域请求。请勿在源 URL 末尾包含“/”

三、 [EnableCors]的作用域规则

可以具体到方法、控制器、全集

方法

下面的示例仅为 GetItem 方法启用 CORS。

public class ItemsController : ApiController
{
    public HttpResponseMessage GetAll() { ... }

    [EnableCors(origins: "http://www.example.com", headers: "*", methods: "*")]
    public HttpResponseMessage GetItem(int id) { ... }

    public HttpResponseMessage Post() { ... }
    public HttpResponseMessage PutItem(int id) { ... }
}

3.1 控制器

示例为除 PutItem以外的每种方法启用 CORS

[EnableCors(origins: "http://www.example.com", headers: "*", methods: "*")]
public class ItemsController : ApiController
{
    public HttpResponseMessage GetAll() { ... }
    public HttpResponseMessage GetItem(int id) { ... }
    public HttpResponseMessage Post() { ... }

    [DisableCors]
    public HttpResponseMessage PutItem(int id) { ... }
}

[DisableCors] 特性添加到方法上,表示取消该方法的跨域

3.2 全局

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("www.example.com", "*", "*");
        config.EnableCors(cors);
        // ...
    }
}

4. 设置允许来源

[EnableCors]属性的源参数允许多个来源访问该资源,多个来源之间用逗号分隔。

[EnableCors(origins: "http://www.contoso.com,http://www.example.com", 
    headers: "*", methods: "*")]

你还可以使用通配符值 “*” 允许来自任何来源的请求。
在允许来自任何来源的请求之前,请仔细考虑。 这意味着,任何网站都可以对 web API 进行 AJAX 调用

// Allow CORS for all origins. (Caution!)
[EnableCors(origins: "*", headers: "*", methods: "*")]

5. 设置允许的HTTP的方法

[EnableCors] methods属性允许哪些HTTP请求访问该资源,若要允许所有的,使用通配符“*”。下面的示例只允许 GET 和 POST 请求。

[EnableCors(origins: "http://www.example.com", headers: "*", methods: "get,post")]
public class TestController : ApiController
{
    public HttpResponseMessage Get() { ... }
    public HttpResponseMessage Post() { ... }
    public HttpResponseMessage Put() { ... }    
}

6. 设置允许的请求标头

[EnableCors] header特性的参数指定允许的请求标头,若要允许任何标头,请将标头设置为 “*”。 若要将特定标头列入允许列表,请将标头设置为允许的标头的逗号分隔列表

[EnableCors(origins: "http://example.com", 
    headers: "accept,content-type,origin,x-my-header", methods: "*")]

参考文献

[https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值