Skip to content

Instantly share code, notes, and snippets.

@railson-ferreira
Created March 12, 2025 13:31
Show Gist options
  • Select an option

  • Save railson-ferreira/0ef6bd337a96a605c0146f4cc318b128 to your computer and use it in GitHub Desktop.

Select an option

Save railson-ferreira/0ef6bd337a96a605c0146f4cc318b128 to your computer and use it in GitHub Desktop.
C# OpenId Connect Dicovery Helper
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