Skip to content

Instantly share code, notes, and snippets.

@bogdanbujdea
Last active September 14, 2021 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdanbujdea/e1f12436b3de7964ccffd8dc23243328 to your computer and use it in GitHub Desktop.
Save bogdanbujdea/e1f12436b3de7964ccffd8dc23243328 to your computer and use it in GitHub Desktop.
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