Skip to content

Commit

Permalink
Move CORS to Configure.Cors.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Feb 20, 2024
1 parent 8dce48e commit a88f5aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
33 changes: 33 additions & 0 deletions TechStacks/Configure.Cors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[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", "http://localhost:8080",
"https://localhost:5173", "http://localhost:5173",
"http://run.plnkr.co", "http://null.jsbin.com",
])
.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);
};
}
}
13 changes: 0 additions & 13 deletions TechStacks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@

services.AddAuthorization();

services.AddCors(options => {
options.AddDefaultPolicy(policy => {
policy.WithOrigins([
"http://localhost:5000", "https://localhost:5001", "http://localhost:8080",
"https://localhost:5173", "http://localhost:5173",
"http://run.plnkr.co", "http://null.jsbin.com",
])
.AllowCredentials()
.WithHeaders(["Content-Type", "Allow", "Authorization"])
.SetPreflightMaxAge(TimeSpan.FromHours(1));
});
});

// $ dotnet ef migrations add CreateIdentitySchema
// $ dotnet ef database update
var connectionString = Environment.GetEnvironmentVariable("TECHSTACKS_DB") ??
Expand Down

0 comments on commit a88f5aa

Please sign in to comment.