0 votes
by (2.5k points)
retagged by

I think I've implemented CORS correctly. I added the .NET CORS package, and my ConfigureServices has this : 

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(); // Make sure you call this previous to AddMvc
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

And my Configure method has this : 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvc();
	
    app.UseCors(
        options => options.WithAnyOrigin().AllowAnyMethod()
    );
}

But I can see in Chrome that it is not allowing any origin or sending CORS headers in return

1 Answer

+1 vote
by (2.5k points)
edited by

Your issue is that the Configure method runs in order, and specifically, some methods in your Configure method will actually short circuit and return a result. 

In your case, the call to UseMVC() will return before the call to UseCors(). So you just need to reverse the order like so : 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseCors(
        options => options.WithAnyOrigin().AllowAnyMethod()
    );

    app.UseMvc();
}

 

Welcome to .NET Q&A, where you can ask questions and receive answers from other members of the community.
...