From b8cf5260221006cc854b3e15f0e74ff49ae080b8 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 4 Apr 2026 09:20:37 -0400 Subject: [PATCH] Fix OIDC redirect_uri mismatch behind k8s ingress (SOR.Admin) The admin pod runs plain HTTP; the ingress terminates TLS. Without forwarded headers, ASP.NET Core built the redirect_uri as "http://control.aeternalisgames.com/signin-oidc" but Keycloak only accepts the https:// variant, causing "invalid_redirect_uri". Add UseForwardedHeaders() with XForwardedProto/Host/For before UseAuthentication so the OIDC middleware sees the correct public https scheme and hostname when constructing the callback URL. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Server/src/SOR.Admin/Program.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Server/src/SOR.Admin/Program.cs b/Server/src/SOR.Admin/Program.cs index ca57472..7c7820a 100644 --- a/Server/src/SOR.Admin/Program.cs +++ b/Server/src/SOR.Admin/Program.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -127,9 +128,25 @@ builder.Services.AddChatDatabase(builder.Configuration); builder.Services.AddGameDatabase(builder.Configuration); builder.Services.AddSupportDatabase(builder.Configuration); +// ── Forwarded headers ───────────────────────────────────────────────────── +// The pod runs plain HTTP behind a TLS-terminating ingress. Without this, +// ASP.NET Core builds the OIDC redirect_uri as "http://..." which Keycloak +// rejects. Clearing KnownNetworks/KnownProxies trusts any upstream proxy — +// safe because the pod is only reachable from within the cluster. +builder.Services.Configure(options => +{ + options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost; + options.KnownNetworks.Clear(); + options.KnownProxies.Clear(); +}); + // ── HTTP pipeline ───────────────────────────────────────────────────────── var app = builder.Build(); +// Must come before UseAuthentication so the scheme/host are correct when +// the OIDC middleware constructs the redirect_uri. +app.UseForwardedHeaders(); + if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error");