Trouble with CORS Policy and .NET Core 3.1 Trouble with CORS Policy and .NET Core 3.1 angular angular

Trouble with CORS Policy and .NET Core 3.1


first app.UseRouting(); then app.UseCors("foo");

Change your Configure method like the following :

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){    if (env.IsDevelopment())    {        app.UseDeveloperExceptionPage();    }    app.UseHttpsRedirection();    app.UseRouting();  // first    // Use the CORS policy    app.UseCors("foo"); // second    app.UseAuthorization();    app.UseEndpoints(endpoints =>    {        endpoints.MapControllers();    });}

It worked for me !


Web API is using app.UseHttpsRedirection(); which cause CORS issue if requesting client is not https based. So in order to use it with http client we need to comment or remove that line.

This issue is not with CORS, the https is causing this issue but thrown error is saying its with CORS.


When adding the Cors Service make sure to include .SetIsOriginAllowed((host) => true) after .WithOrigins("http://localhost:4200")

services.AddCors(options => {            options.AddPolicy("mypolicy",builder => builder            .WithOrigins("http://localhost:4200/")            .SetIsOriginAllowed((host) => true)            .AllowAnyMethod()            .AllowAnyHeader());  });