Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active February 20, 2024 09:41
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 gistlyn/17a6dfae71fc88be8f7428fcad32614e to your computer and use it in GitHub Desktop.
Save gistlyn/17a6dfae71fc88be8f7428fcad32614e to your computer and use it in GitHub Desktop.
Configure support for CORS
[assembly: HostingStartup(typeof(MyApp.ConfigureCors))]
namespace MyApp;
public class ConfigureCors : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services =>
{
services.AddCors(options => {
options.AddDefaultPolicy(policy => {
policy.WithOrigins([
"http://localhost:5000", "https://localhost:5001",
"https://localhost:5173", "http://localhost:5173",
])
.AllowCredentials()
.WithHeaders(["Content-Type", "Allow", "Authorization"])
.SetPreflightMaxAge(TimeSpan.FromHours(1));
});
});
services.AddTransient<IStartupFilter, StartupFilter>();
});
public class StartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
{
app.UseCors();
next(app);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment