fix: update CORS policy and add CorsOrigins to configuration #5

This commit is contained in:
Marek Lesko
2025-08-01 11:02:57 +02:00
parent f8c555bc84
commit 4d26344665
3 changed files with 15 additions and 6 deletions

View File

@@ -33,6 +33,7 @@ docker-build:
script: script:
- 'sed -i "s|\"apiEndpoint\": \"[^\"]*\"|\"apiEndpoint\": \"https\:\/\/""$PUBLIC_WEB_URL""\"|" Web/public/config.json' - 'sed -i "s|\"apiEndpoint\": \"[^\"]*\"|\"apiEndpoint\": \"https\:\/\/""$PUBLIC_WEB_URL""\"|" Web/public/config.json'
- 'sed -i "s|\"AllowedHosts\": \"[^\"]*\"|\"AllowedHosts\": \"$PUBLIC_WEB_URL\"|" Api/appsettings.json' - 'sed -i "s|\"AllowedHosts\": \"[^\"]*\"|\"AllowedHosts\": \"$PUBLIC_WEB_URL\"|" Api/appsettings.json'
- 'sed -i "s|\"CorsOrigins\": \"[^\"]*\"|\"CorsOrigins\": \"https\:\/\/""$PUBLIC_WEB_URL""\"|" Api/appsettings.json'
- docker build -t $CI_REGISTRY_IMAGE:latest -t $CI_REGISTRY_IMAGE:${CI_PIPELINE_IID} -f Dockerfile . - docker build -t $CI_REGISTRY_IMAGE:latest -t $CI_REGISTRY_IMAGE:${CI_PIPELINE_IID} -f Dockerfile .
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
- docker push $CI_REGISTRY_IMAGE:latest - docker push $CI_REGISTRY_IMAGE:latest

View File

@@ -24,7 +24,6 @@ namespace Api
}) })
.AddJwtBearer(options => .AddJwtBearer(options =>
{ {
options.Authority = builder.Configuration["Authentication:PocketId:Authority"]; options.Authority = builder.Configuration["Authentication:PocketId:Authority"];
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{ {
@@ -35,9 +34,14 @@ namespace Api
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowAll", policy => options.AddPolicy("Default", policy =>
{ {
policy.AllowAnyOrigin() var allowedHostsConfiguration = builder.Configuration["CorsOrigins"]?
.ToString()
.Split(',');
policy
.WithOrigins(allowedHostsConfiguration ?? new[] { "*" })
.AllowAnyHeader() .AllowAnyHeader()
.AllowAnyMethod(); .AllowAnyMethod();
}); });
@@ -52,6 +56,7 @@ namespace Api
: builder.Configuration.GetConnectionString("DefaultConnection"); : builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options => builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(connectionString)); options.UseSqlServer(connectionString));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
@@ -67,6 +72,7 @@ namespace Api
app.UseHttpsRedirection(); app.UseHttpsRedirection();
} }
// Angular rewrite for SPA hosting
var routes = new[] { "api", "swagger" }; var routes = new[] { "api", "swagger" };
var rewriteString = String.Join("|", routes); var rewriteString = String.Join("|", routes);
var rewriteOptions = new RewriteOptions() var rewriteOptions = new RewriteOptions()
@@ -95,7 +101,7 @@ namespace Api
app.UseStaticFiles(); app.UseStaticFiles();
} }
app.UseCors("AllowAll"); app.UseCors("Default");
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();

View File

@@ -14,5 +14,7 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "localhost",
"CorsOrigins": "https://localhost:5001,http://localhost:4200,http://localhost:5000"
} }