赞
踩
浏览器阻止Web页面向另一个域中发送请求的一种安全策略,此限制称为同源策略,可防止恶意站点读取另一个站点的铭感数据。
跨资源共享(CORS),允许服务器放松同源策略,使用CORS,服务器可以显示的允许某些跨域请求而拒绝其他的跨域请求。
同源策略不会阻止浏览器发送请求。 相反,它会阻止应用程序查看响应。
协议、IP、端口相同即为同源
同源:
非同源:
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 末尾包含“/”
可以具体到方法、控制器、全集
下面的示例仅为 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) { ... }
}
示例为除 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] 特性添加到方法上,表示取消该方法的跨域
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("www.example.com", "*", "*");
config.EnableCors(cors);
// ...
}
}
[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: "*")]
[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() { ... }
}
[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]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。