2 Commits

Author SHA1 Message Date
khwezi 029f5b5d8a Merge pull request 'Refactored forced https redirection' (#15) from notifications into master
Reviewed-on: #15
2026-05-17 11:35:49 +02:00
Khwezi Mngoma ff81325020 Refactored forced https redirection
continuous-integration/drone/pr Build is passing
2026-05-17 11:34:40 +02:00
+11 -6
View File
@@ -65,15 +65,18 @@ builder.Services.AddAuthentication(options =>
RoleClaimType = "groups" RoleClaimType = "groups"
}; };
builder.Services.AddOptions<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme).Configure(options => options.Events = new OpenIdConnectEvents
{ {
options.Events.OnRedirectToIdentityProvider = context => OnRedirectToIdentityProvider = context =>
{ {
context.ProtocolMessage.RedirectUri = context.ProtocolMessage.RedirectUri.Replace("http://", "https://"); var fallbackUri = context.ProtocolMessage.RedirectUri;
if (fallbackUri.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
context.ProtocolMessage.RedirectUri = fallbackUri.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase);
return Task.CompletedTask; return Task.CompletedTask;
}; }
}); };
}); });
builder.Services.Configure<ForwardedHeadersOptions>(options => builder.Services.Configure<ForwardedHeadersOptions>(options =>
@@ -90,6 +93,8 @@ var scheduler = await schedulerFactory.GetScheduler(ShopSchedulerName);
if (!scheduler!.IsStarted) if (!scheduler!.IsStarted)
await scheduler.Start(); await scheduler.Start();
app.UseForwardedHeaders();
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseExceptionHandler("/Error", createScopeForErrors: true);
@@ -105,7 +110,6 @@ app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages:
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAntiforgery(); app.UseAntiforgery();
app.UseForwardedHeaders();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
@@ -114,6 +118,7 @@ app.MapStaticAssets();
app.MapGet("/auth/login", (string redirectUri = "/") => app.MapGet("/auth/login", (string redirectUri = "/") =>
Results.Challenge(new AuthenticationProperties { RedirectUri = redirectUri }, [OpenIdConnectDefaults.AuthenticationScheme])); Results.Challenge(new AuthenticationProperties { RedirectUri = redirectUri }, [OpenIdConnectDefaults.AuthenticationScheme]));
app.MapGet("/auth/logout", async (HttpContext context) => app.MapGet("/auth/logout", async (HttpContext context) =>
{ {
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);