I am trying to enable "*" CORS headers on application startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(opts =>
{
opts.Filters.Add(new AuthorizationFilterFactory());
});
services.AddCors(opts => opts.AddPolicy("*",
builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowCredentials()));
services.AddSwaggerGen();
services.ConfigureSwaggerGen(opts =>
{
opts.SingleApiVersion(new Info { Version = "v1", Title = "CORS app" });
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("*");
app.UseSwagger();
app.UseSwaggerUi();
app.UseMiddleware(typeof(ExceptionHandlingMiddleware));
app.UseMvc();
}
The headers don't show up. Tried to request my resources with Ajax from a different domain and I am getting:
415 (Unsupported Media Type)
Tried with JS:
$.post("http://example.com/some-api", JSON.stringify({"name": "hello"}));
Any ideas? Maybe something wrong with the order of middleware registrations?
UPDATE: Apparently this CORS library adds headers only when it identifies XHR cross-origin requests. It suddenly started working for me. Thanks everyone for your comments.