Enable CORS on all endpoints
This commit is contained in:
24
Program.cs
24
Program.cs
@@ -30,9 +30,23 @@ try
|
||||
if (apiKeysTemp != null) apiKeys = apiKeysTemp;
|
||||
}
|
||||
}
|
||||
catch {}
|
||||
catch { }
|
||||
builder.Services.AddSingleton<IApiKeyValidator, ApiKeyValidator>(_ => new ApiKeyValidator(apiKeys));
|
||||
|
||||
//setup CORS if origins were supplied in the config file
|
||||
string[]? allowedOrigins = builder.Configuration.GetValue<string[]>("AllowedOrigins");
|
||||
if (allowedOrigins != null)
|
||||
{
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: "AllowedOrigins",
|
||||
policy =>
|
||||
{
|
||||
policy.WithOrigins(allowedOrigins);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
@@ -64,13 +78,21 @@ builder.Services.AddSwaggerGen(c =>
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
//
|
||||
// Configure the HTTP request pipeline.
|
||||
//
|
||||
|
||||
// if (app.Environment.IsDevelopment())
|
||||
// {
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
// }
|
||||
|
||||
if (allowedOrigins != null)
|
||||
{
|
||||
app.UseCors();
|
||||
}
|
||||
|
||||
app.UseAuthentication();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
Reference in New Issue
Block a user