我已经有一个多小时的CORS配置问题了,我找不到一个可以帮助我了解这种情况的问题。
我在带有预检OPTIONS
请求的登台域上触发了XHR,该请求以400返回,并且未发送POST,因为不存在允许它的CORS标头。
我的config.EnableCors();
中有WebApiConfig.cs
,并且正在使用NuGet包Microsoft ASP.NET Web API 2.2 Cross-Origin Support
(Microsoft.AspNet.WebApi.Cors v5.23)
然后,我已经设置了默认值,而不是使用空的构造函数,因为看到了this问题,所以WebApiConfig
行是:
var defaultCors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(defaultCors);
我对此不介意,但我宁愿完全不在此处设置任何内容。无论如何,这是行不通的。
当
OPTIONS
请求由Global.asax.cs
处理但未写入标头时,错误如下所示:OPTIONS http://staging.example.com:8044/Controller/Action
XMLHttpRequest cannot load http://staging.example.com:8044/Controller/Action. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://staging.example.co.uk:8044' is therefore not allowed access.
Global.asax.cs
中未处理请求时,错误如下所示:OPTIONS http://staging.example.com:8044
XMLHttpRequest cannot load http://staging.example.com:8044/Controller/Action. Invalid HTTP status code 400
通过使用
Global.asax.cs
中的以下解决方案,我已经能够访问API跨域:protected void Application_BeginRequest()
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://staging.example.com:8044");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Accepts, Content-Type, Origin, X-My-Header");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "60");
HttpContext.Current.Response.End();
}
}
}
我实际上想做的是这样的:
[EnableCors("http://www.example.com", "*", "get,post")]
public sealed class ControllerController : ApiController
{
[EnableCors("http://staging.example.com:8044,http://www.example.com", "*", "post")]
[HttpPost]
public async Task<HttpResponseMessage> Action([FromBody] ModelledInput input)
{
...
}
}
尽管我理解
BeginRequest
的原因,但我不明白为什么CORS属性没有处理预检请求。我希望在控制器或操作级别(而不是全局)上管理CORS控件。我以为可能是HttpPost
属性,但是删除该属性无效。我乐意接受我做错了什么或者请求可能被过滤或更高级别处理的建议-我不确定在哪里可以解决这个问题,但是我不乐意在全球范围内处理CORS。
我将不胜感激!
最佳答案
protected void Application_BeginRequest()
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://staging.example.com:8044");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Accepts, Content-Type, Origin, X-My-Header");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "60");
HttpContext.Current.Response.End();
}
}
上面提到的代码对我有用
关于c# - 预检失败时如何通过属性而不是通过Application_BeginRequest启用CORS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31459416/