From 33b544a404562b0085a503e1c0a96ab2c6a45b56 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 4 Apr 2026 12:54:27 -0400 Subject: [PATCH] Pin static OIDC config at startup to fix per-request discovery failures Replaces the default ConfigurationManager with a FixedConfigurationManager that returns the pre-fetched OpenIdConnectConfiguration synchronously, so per-request GetConfigurationAsync can never fail due to network errors or cancellation-token / lock-contention issues in the dynamic manager. 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 | 20 ++++++++++++++++++-- Server/src/SOR.Admin/SOR.Admin.csproj | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Server/src/SOR.Admin/Program.cs b/Server/src/SOR.Admin/Program.cs index 88de330..3d111e6 100644 --- a/Server/src/SOR.Admin/Program.cs +++ b/Server/src/SOR.Admin/Program.cs @@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.JsonWebTokens; +using Microsoft.IdentityModel.Protocols; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Tokens; using StackExchange.Redis; using SOR.Admin.Components; @@ -256,8 +258,11 @@ static async Task CheckOidcDiscoveryAsync(WebApplication app) ?? $"{opts.Authority}/.well-known/openid-configuration"; try { - await opts.ConfigurationManager!.GetConfigurationAsync(CancellationToken.None); - app.Logger.LogInformation("Keycloak OIDC discovery OK: {Url}", url); + var config = await opts.ConfigurationManager!.GetConfigurationAsync(CancellationToken.None); + // Pin the fetched config so per-request GetConfigurationAsync never does a + // live network call (or hits lock contention with a cancellation token). + opts.ConfigurationManager = new FixedConfigurationManager(config); + app.Logger.LogInformation("Keycloak OIDC discovery OK: {AuthEndpoint}", config.AuthorizationEndpoint); } catch (Exception ex) { @@ -296,3 +301,14 @@ static void AddKeycloakRoleClaims(ClaimsIdentity identity, string accessToken) } } } + +// Returns a pre-fetched OpenIdConnectConfiguration on every call so the OIDC +// middleware never makes a network request per browser request (or fails due to +// cancellation-token / lock-contention issues in the default ConfigurationManager). +sealed class FixedConfigurationManager(OpenIdConnectConfiguration config) + : IConfigurationManager +{ + public Task GetConfigurationAsync(CancellationToken cancel) + => Task.FromResult(config); + public void RequestRefresh() { } +} diff --git a/Server/src/SOR.Admin/SOR.Admin.csproj b/Server/src/SOR.Admin/SOR.Admin.csproj index 8c10a76..a96dde5 100644 --- a/Server/src/SOR.Admin/SOR.Admin.csproj +++ b/Server/src/SOR.Admin/SOR.Admin.csproj @@ -7,6 +7,7 @@ +