Last active
February 20, 2024 09:41
-
-
Save gistlyn/17a6dfae71fc88be8f7428fcad32614e to your computer and use it in GitHub Desktop.
Configure support for CORS
This file contains hidden or 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
[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