using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Http.Extensions;
public const string OptionsJson = """
"OAuth": "/.well-known/oauth-protected-resource/{resource}",
"Jwks": "/.well-known/jwks"
public static void Main()
Uri test = new ("https://example.com/test");
Uri test1 = new ("https://example.com");
Uri oauth = new (ProtectedResourceConstants.DefaultOAuthProtectedResourcePathSuffix);
Uri jwks = new (ProtectedResourceConstants.JsonWebKeySetPathSuffix);
IServiceCollection coll = new ServiceCollection();
var options = JsonSerializer.Deserialize<TestOptions>(OptionsJson);
var builder = new UriBuilder(
scheme: Uri.UriSchemeHttps,
Console.WriteLine(oauth.IsAbsoluteUri);
Console.WriteLine(jwks.IsAbsoluteUri);
Console.WriteLine(options.OAuth.IsAbsoluteUri);
Console.WriteLine(options.Jwks.IsAbsoluteUri);
Console.WriteLine(builder.Uri);
public string Name {get; set;}
public Uri OAuth {get; set; }
public Uri Jwks {get; set;}
public class Test : ITest
public Test([ServiceKey] string? serviceKey = null)
Name = serviceKey ?? "default";
public class ExternalService(ITest testService, IOptionsMonitor<TestOptions> monitor) : IExternalService
public string GetOptions() => monitor.GetKeyedOrCurrent("keyed").Name;
public string GetNameExt()
return testService.GetName();
public interface IExternalService
public string GetNameExt();
public string GetOptions();
internal static class ServiceProviderExtensions
public static TOptions GetKeyedOrCurrent<TOptions>(
this IOptionsMonitor<TOptions> optionsMonitor, string? serviceKey)
return optionsMonitor.CurrentValue;
return optionsMonitor.Get(serviceKey);
public static T GetRequiredOrKeyedRequiredService<T>(this IServiceProvider provider, string? serviceKey)
return serviceKey is null
? provider.GetRequiredService<T>()
: provider.GetRequiredKeyedService<T>(serviceKey);
public static class ProtectedResourceConstants
public const string DefaultOAuthProtectedResourcePathSuffix = "/.well-known/oauth-protected-resource/{resource}";
public const string JsonWebKeySetPathSuffix = "/.well-known/jwks";
public static class WWWAuthenticateKeys
public const string UnsignedResourceMetadata = "resource_metadata";
public const string SignedResourceMetadata = "signed_metadata";
public static class AuthenticationSchemes
public const string DPoP = "DPoP";