Skip to content

Commit a88f5aa

Browse files
committed
Move CORS to Configure.Cors.cs
1 parent 8dce48e commit a88f5aa

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

TechStacks/Configure.Cors.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[assembly: HostingStartup(typeof(MyApp.ConfigureCors))]
2+
3+
namespace MyApp;
4+
5+
public class ConfigureCors : IHostingStartup
6+
{
7+
public void Configure(IWebHostBuilder builder) => builder
8+
.ConfigureServices(services =>
9+
{
10+
services.AddCors(options => {
11+
options.AddDefaultPolicy(policy => {
12+
policy.WithOrigins([
13+
"http://localhost:5000", "https://localhost:5001", "http://localhost:8080",
14+
"https://localhost:5173", "http://localhost:5173",
15+
"http://run.plnkr.co", "http://null.jsbin.com",
16+
])
17+
.AllowCredentials()
18+
.WithHeaders(["Content-Type", "Allow", "Authorization"])
19+
.SetPreflightMaxAge(TimeSpan.FromHours(1));
20+
});
21+
});
22+
services.AddTransient<IStartupFilter, StartupFilter>();
23+
});
24+
25+
public class StartupFilter : IStartupFilter
26+
{
27+
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
28+
{
29+
app.UseCors();
30+
next(app);
31+
};
32+
}
33+
}

TechStacks/Program.cs

-13
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,6 @@
3737

3838
services.AddAuthorization();
3939

40-
services.AddCors(options => {
41-
options.AddDefaultPolicy(policy => {
42-
policy.WithOrigins([
43-
"http://localhost:5000", "https://localhost:5001", "http://localhost:8080",
44-
"https://localhost:5173", "http://localhost:5173",
45-
"http://run.plnkr.co", "http://null.jsbin.com",
46-
])
47-
.AllowCredentials()
48-
.WithHeaders(["Content-Type", "Allow", "Authorization"])
49-
.SetPreflightMaxAge(TimeSpan.FromHours(1));
50-
});
51-
});
52-
5340
// $ dotnet ef migrations add CreateIdentitySchema
5441
// $ dotnet ef database update
5542
var connectionString = Environment.GetEnvironmentVariable("TECHSTACKS_DB") ??

0 commit comments

Comments
 (0)