Asp.net web api Cors在web api 2.0中不起作用
我正在努力理解并在WebAPI项目中启用CORS。我碰到了一个障碍点。我从一个具有ASP.NET标识的ASP.NET MVC Web Api 2项目开始。无论我做什么似乎都不管用 我已经删除了我的Asp.net web api Cors在web api 2.0中不起作用,asp.net-web-api,cors,token,owin,Asp.net Web Api,Cors,Token,Owin,我正在努力理解并在WebAPI项目中启用CORS。我碰到了一个障碍点。我从一个具有ASP.NET标识的ASP.NET MVC Web Api 2项目开始。无论我做什么似乎都不管用 我已经删除了我的global.asx文件,我的启动如下所示: public partial class Startup { public void Configuration(IAppBuilder app) { HttpConfiguration configuration = n
global.asx
文件,我的启动如下所示:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration configuration = new HttpConfiguration();
// I'm not sure it this is the proper way to use webapiconfig
WebApiConfig.Register(configuration);
app.UseWebApi(configuration);
app.UseCors(CorsOptions.AllowAll);
ConfigureAuth(app);
}
}
WebApiConfig.Register代码为:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.AddODataQueryFilter();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
RegisterModels(); //Automapper here
}
我只调用app.UseCors(CorsOptions.AllowAll)来启用CORS,没有其他方法像config.enableCors
或其他任何方法,但是每当我尝试获取令牌或API中的任何内容时,我都会得到错误:
原因:缺少CORS标头“访问控制允许来源”
我已经尝试在
配置方法中设置断点,但它没有被调用。。。曾经我正在调试IIS Express 对我来说什么都不管用。。经过多次尝试,我终于找到了工作
如果你有同样的问题
1) 从安装的nugget软件包中删除任何与cors相关的内容。。一切
2) 从web.config中删除任何与cors相关的内容
3) 在Gloabal.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
var context = HttpContext.Current;
var response = context.Response;
response.AddHeader("Access-Control-Allow-Origin", "*");
response.AddHeader("X-Frame-Options", "ALLOW-FROM *");
if (context.Request.HttpMethod == "OPTIONS")
{
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PATCH, PUT");
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
response.AddHeader("Access-Control-Max-Age", "1000000");
response.End();
}
}
这对/api和/token都适用。
这是一个通用解决方案,在将其部署到prod之前请注意
希望将帮助所有有同样问题的人。这是唯一对我有效的解决方案。谢谢非常感谢,关于如何进行生产部署有什么建议吗?嗨,Lobato,如果您知道您的呼叫将从哪里来,那么请更改*该站点的来源。因为*现在的意思是“任何地方”。另外,一些API只允许从源代码外部“获取”,您必须使用它们的工具进行CRUD操作。您可以忽略更改“访问控制最大年龄”,因为它将被特定于浏览器的值覆盖。(例如铬合金中的10分钟)。
protected void Application_BeginRequest(object sender, EventArgs e)
{
var context = HttpContext.Current;
var response = context.Response;
response.AddHeader("Access-Control-Allow-Origin", "*");
response.AddHeader("X-Frame-Options", "ALLOW-FROM *");
if (context.Request.HttpMethod == "OPTIONS")
{
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PATCH, PUT");
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
response.AddHeader("Access-Control-Max-Age", "1000000");
response.End();
}
}