Created
March 12, 2025 13:31
-
-
Save railson-ferreira/0ef6bd337a96a605c0146f4cc318b128 to your computer and use it in GitHub Desktop.
C# OpenId Connect Dicovery Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Concurrent; | |
| namespace Gbt.Identity.RavenDb.OpenId; | |
| public class OpenIdConnectHelper(string Issuer) | |
| { | |
| private static readonly ConcurrentDictionary<string, OpenIdConfiguration> Cache = new(); | |
| public record OpenIdConfiguration(string authorization_endpoint, string token_endpoint); | |
| public async Task<OpenIdConfiguration> Discover(CancellationToken cancellationToken = default) | |
| { | |
| if (Cache.TryGetValue(Issuer, out var cachedConfig)) | |
| return cachedConfig; | |
| var httpClient = new HttpClient(); | |
| var oidcDiscovery = new UriBuilder(Issuer); | |
| oidcDiscovery.Path = "/.well-known/openid-configuration"; | |
| var response = await httpClient.GetAsync(oidcDiscovery.Uri, cancellationToken); | |
| var config = await response.Content.ReadFromJsonAsync<OpenIdConfiguration>(cancellationToken: cancellationToken); | |
| if (Cache.TryAdd(Issuer, config)) | |
| _ = Task.Delay(TimeSpan.FromMinutes(10)).ContinueWith(_ => Cache.Remove(Issuer, out var _)); | |
| return config; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment