Last active
September 14, 2021 20:14
-
-
Save bogdanbujdea/e1f12436b3de7964ccffd8dc23243328 to your computer and use it in GitHub Desktop.
Enable CORS in ASP.NET Core Web Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
readonly string MyAllowSpecificOrigins = "randomPolicyNameUsedForTestingPurposes"; //the name doesn't matter | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddCors(options => | |
{ | |
options.AddPolicy(name: MyAllowSpecificOrigins, | |
builder => | |
{ | |
builder.WithOrigins("*"); // allow all, I just want to enable CORS without bothering to specify all the allowed origins | |
}); | |
}); | |
} | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
// here we use our policy to enable CORS | |
app.UseCors(MyAllowSpecificOrigins); | |
// routing code, bla bla | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment