Reputation: 568
I have ASP.NET 5 web site (dnx451) hosted in Azure. It works fine for an hour or so after the deployment and then CORS feature stop working. Other services like DI, Authorization and MVC are still functioning.
Here is Startup.cs code:
public void ConfigureServices(IServiceCollection services)
{
configureCors(services);
services.AddMvc(options =>
{
options.Filters.Add(new ErrorFilter());
});
}
private static void configureCors(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(x => x.AddPolicy("allowAll", p=>p.AllowAnyOrigin().
AllowAnyHeader().AllowAnyMethod()));
}
App configuration:
public void Configure(IApplicationBuilder app, IApplicationEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions());
app.UseErrorPage();
// Add MVC to the request pipeline.
app.UseMvc();
app.UseCors("allowAll");
}
Local version has CORS all the time but Azure web has it only in the first 1-2 hours (depending on the usage). I guess CORS disappears when Azure restarts the app.
It is strange. Does anyone have the same problem?
Upvotes: 1
Views: 192
Reputation: 568
Thanks to Henk, I was able to fix the problem:
I had both [EnableCors]
attribute and app.UseCors()
startup call. After removing UseCors, the problem disappeared.
However it is still strange why Cors is working temporarily when both options are in place.
Update: As for https://github.com/aspnet/CORS/issues/36
You need not call services.AddCors()
here as AddMvc internally already does that.
Upvotes: 2