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 <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -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<OpenIdConnectConfiguration>
|
||||
{
|
||||
public Task<OpenIdConnectConfiguration> GetConfigurationAsync(CancellationToken cancel)
|
||||
=> Task.FromResult(config);
|
||||
public void RequestRefresh() { }
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" />
|
||||
|
||||
<!-- Design-time only: enables 'dotnet ef migrations add' targeting this project -->
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
|
||||
Reference in New Issue
Block a user