Skip to content

Instantly share code, notes, and snippets.

@thewindev
Last active Sep 14, 2021
Embed
What would you like to do?
Enable CORS in ASP.NET Core Web Api
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