diff --git a/Server/src/SOR.Admin/Program.cs b/Server/src/SOR.Admin/Program.cs index de4f3e9..88de330 100644 --- a/Server/src/SOR.Admin/Program.cs +++ b/Server/src/SOR.Admin/Program.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -223,6 +224,12 @@ app.MapRazorComponents() // ── Apply pending migrations ─────────────────────────────────────────────── await MigrateAllAsync(app); +// ── Eager OIDC discovery ─────────────────────────────────────────────────── +// Fetch the Keycloak discovery document at startup so any network/DNS failure +// surfaces as a clear log error rather than the cryptic "Cannot redirect to +// the authorization endpoint" message on the first browser request. +await CheckOidcDiscoveryAsync(app); + app.Run(); // ── Helpers ──────────────────────────────────────────────────────────────── @@ -240,6 +247,27 @@ static async Task MigrateAllAsync(WebApplication app) await sp.GetRequiredService().Database.MigrateAsync(); } +static async Task CheckOidcDiscoveryAsync(WebApplication app) +{ + var opts = app.Services + .GetRequiredService>() + .Get("Keycloak"); + var url = opts.MetadataAddress + ?? $"{opts.Authority}/.well-known/openid-configuration"; + try + { + await opts.ConfigurationManager!.GetConfigurationAsync(CancellationToken.None); + app.Logger.LogInformation("Keycloak OIDC discovery OK: {Url}", url); + } + catch (Exception ex) + { + app.Logger.LogError(ex, + "Keycloak OIDC discovery FAILED from {Url}. " + + "If the pod cannot reach the public auth domain, set " + + "Keycloak__MetadataAddress to the internal k8s service URL.", url); + } +} + // Decodes the Keycloak access token and adds realm_access.roles and groups // as ClaimTypes.Role claims. Called from OnTicketReceived where SaveTokens // has already run, so the token is guaranteed present in auth properties.