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 @@ +