Log actual OIDC discovery failure at startup (SOR.Admin)

Eagerly fetch the Keycloak discovery document after app.Build() so any
network/DNS/SSL failure appears in startup logs with the real exception and
the exact URL being fetched, rather than surfacing later as the cryptic
"Cannot redirect to the authorization endpoint" message.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-04 12:28:05 -04:00
co-authored by Claude Happy
parent d6b99a9fe5
commit 63f25a3764
+28
View File
@@ -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<App>()
// ── 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<SupportDbContext>().Database.MigrateAsync();
}
static async Task CheckOidcDiscoveryAsync(WebApplication app)
{
var opts = app.Services
.GetRequiredService<IOptionsMonitor<OpenIdConnectOptions>>()
.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.