Cors 访问控制允许源-多个域访问MVC web api 2

Cors 访问控制允许源-多个域访问MVC web api 2,cors,asp.net-web-api2,Cors,Asp.net Web Api2,我需要从我的web API允许来自多个域的CORS请求。然而,我不想通过在web配置中执行以下操作来让它为所有人打开 但是,我不希望使用.htaccess文件,因为我无法控制IIS服务器 我试了又试 问题是,它总是返回以下错误 请求的资源上不存在“Access Control Allow Origin”标头 这是我的实现 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple=true,Inh

我需要从我的web API允许来自多个域的CORS请求。然而,我不想通过在web配置中执行以下操作来让它为所有人打开


但是,我不希望使用
.htaccess
文件,因为我无法控制IIS服务器

我试了又试

问题是,它总是返回以下错误

请求的资源上不存在“Access Control Allow Origin”标头

这是我的实现

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple=true,Inherited=true)]
公共类EnableCORSAttribute:ActionFilterAttribute
{
私有字符串[]来源;
私有字符串[]方法;
public EnableCORSAttribute(string[]origins=null,string[]methods=null)
{
这个。起源=起源;
这个方法=方法;
}
公共覆盖无效OnActionExecuted(ActionExecuteContext ActionExecuteContext)
{
if(origins==null | |!origins.Any())
{
//允许所有
HttpContext.Current.Response.AppendHeader(“访问控制允许源代码”、“*”);
}
else if(origins.Contains(HttpContext.Current.Request.Url.Host,StringComparer.InvariantCultureIgnoreCase))
{
//仅在匹配时允许
HttpContext.Current.Response.AppendHeader(“访问控制允许来源”,HttpContext.Current.Request.Url.Host);
}
if(methods==null | |!methods.Any())
{
//允许所有
HttpContext.Current.Response.AppendHeader(“访问控制允许方法“,”*”);
}
else if(methods.Contains(HttpContext.Current.Request.HttpMethod,StringComparer.InvariantCultureIgnoreCase))
{
//只允许指定
HttpContext.Current.Response.AppendHeader(“访问控制允许方法”,HttpContext.Current.Request.HttpMethod);
}
}
}    
这就是我如何用属性装饰我的
BaseController

[EnableCORS(来源:新字符串[]{“localhost”},方法:新字符串[]{“GET”、“POST”、“OPTIONS”}]
公共类BaseController:ApiController
{
//获取api/
公共HttpResponseMessage选项()
{
返回新的HttpResponseMessage{StatusCode=HttpStatusCode.OK};
}
}
我还尝试从
Global.asax
文件中注册此属性

GlobalFilters.Filters.Add(新的EnableCorsAttribute());
RegisterGlobalFilters(GlobalFilters.Filters);
但仍然会得到相同的错误。不知道我还错过了什么

我还尝试了
Microsoft.AspNet.WebApi.Cors
Nuget软件包,并将其放入WebApiConfig文件中

var enableCorsAttribute=新的enableCorsAttribute(“http://localhost:59427",
“来源、内容类型、接受、授权”,
“获取、发布、选项”);
config.EnableCors(enableCorsAttribute);

它适用于一个域,但当我添加另一个域时,它也开始抛出相同的错误。

结果比我想象的要简单

我所需要做的就是查看请求和响应头

我试图添加的另一个域允许
CORS
请求发送一些自定义头

因此,我允许使用这些特定的自定义标题,它起了作用

这是最终的解决方案

My
BaseController

//这里没有华丽的装饰……:)
公共类BaseController:ApiController
{
//获取api/
公共HttpResponseMessage选项()
{
返回新的HttpResponseMessage{StatusCode=HttpStatusCode.OK};
}
}
我的
WebApiConfig

公共静态类WebApiConfig
{
公共静态无效寄存器(HttpConfiguration配置)
{
//这里有一些API路由。。。
var enableCorsAttribute=新的enableCorsAttribute(
//以逗号分隔的允许域白名单,请注意域末尾没有斜杠(“/”)
"http://local.ui.two:9003,http://local.ui.one:9001", 
//允许的自定义标题
“来源,X-Requested-With,内容类型,接受,授权,缓存控制,如果自修改,Pragma”,
//允许的方法
获取、放置、发布、删除、选项
);
config.EnableCors(enableCorsAttribute);
}
}
并在
Global.asax
中注册了此WebApiConfig

受保护的无效应用程序\u Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
  • web.config
  • 不需要编写自定义的花式属性,为什么要重新发明轮子!?:)
  • 仅使用Nuget软件包
    Microsoft.AspNet.WebApi.Cors

这是我要求的,两位站起来帮助其他社区的天才给了我正确的方向。

如果需要,我还可以发布我的Javascript代码和从其他域调用的实际控制器方法。