Skip to content

Instantly share code, notes, and snippets.

@lawrence-laz
Last active January 26, 2021 20:00
Show Gist options
  • Save lawrence-laz/af8cbbc646022c35141b79b4d187682b to your computer and use it in GitHub Desktop.
Save lawrence-laz/af8cbbc646022c35141b79b4d187682b to your computer and use it in GitHub Desktop.
Set up CORS in ASP.NET Core on all controllers/methods.
// NuGet Microsoft.AspNet.Cors (5.2.7)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
options.AddDefaultPolicy(builder =>
builder
.AllowAnyOrigin() // .WithOrigins(...) if .AllowCredentials
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(); // Order is important, look into docs.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment