2 Commits

Author SHA1 Message Date
mathias 6dccfa0bca added keyed singleton for muilti instances
Publish NuGets / build (release) Successful in 21s
2025-08-30 15:39:25 -04:00
david.nguyen 560f562205 fix iscredentialsinheader reverse condition
Publish NuGets / build (release) Successful in 17s
2025-03-17 10:35:18 -04:00
2 changed files with 31 additions and 11 deletions
@@ -31,7 +31,7 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
new ("scopes", string.Join(" ", _scopes)) new ("scopes", string.Join(" ", _scopes))
}; };
if (options.IsCredentialsInHeader) if (false == options.IsCredentialsInHeader)
{ {
formContentKeyValues.AddRange([ formContentKeyValues.AddRange([
new KeyValuePair<string, string>("client_id", options.ClientId), new KeyValuePair<string, string>("client_id", options.ClientId),
@@ -45,8 +45,9 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
Content = formContent Content = formContent
}; };
if (false == options.IsCredentialsInHeader) if (options.IsCredentialsInHeader)
{ {
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{options.ClientId}:{options.ClientSecret}")); var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{options.ClientId}:{options.ClientSecret}"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
} }
@@ -10,34 +10,35 @@ namespace OpenHarbor.JwtTokenManager;
public static class ServiceCollectionExtensions public static class ServiceCollectionExtensions
{ {
private static bool _defaultRegistered = false;
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")] [RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
public static IServiceCollection AddJwtTokenManager( public static IServiceCollection AddJwtTokenManager(
this IServiceCollection services, this IServiceCollection services,
IConfiguration configuration, IConfiguration configuration,
string sectionName, string sectionName,
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null) string? name = null,
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
{ {
if (configuration == null) if (configuration == null)
throw new ArgumentNullException(nameof(configuration)); throw new ArgumentNullException(nameof(configuration));
if (string.IsNullOrWhiteSpace(sectionName)) if (string.IsNullOrWhiteSpace(sectionName))
throw new ArgumentException("Section name must be provided.", nameof(sectionName)); throw new ArgumentException("Section name must be provided.", nameof(sectionName));
services.Configure<JwtTokenManagerOptions>(configuration.GetSection(sectionName));
// Apply the builder options name ??= sectionName;
services.Configure<JwtTokenManagerOptions>(name, configuration.GetSection(sectionName));
var builderOptions = new JwtTokenManagerBuilderOptions(); var builderOptions = new JwtTokenManagerBuilderOptions();
configureBuilderOptions?.Invoke(builderOptions); configureBuilderOptions?.Invoke(builderOptions);
// Register the service services.AddKeyedSingleton<IJwtTokenManagerService>(name, (provider, key) =>
services.AddSingleton<IJwtTokenManagerService>(provider =>
{ {
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>(); var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
var options = optionsMonitor.Get(Options.DefaultName); var options = optionsMonitor.Get(name);
// Apply additional configuration
builderOptions.AdditionalConfiguration?.Invoke(options); builderOptions.AdditionalConfiguration?.Invoke(options);
// Configure cache options
var cacheOptions = new JwtTokenManagerCacheOptions(); var cacheOptions = new JwtTokenManagerCacheOptions();
builderOptions.CacheOptions?.Invoke(cacheOptions); builderOptions.CacheOptions?.Invoke(cacheOptions);
@@ -48,6 +49,24 @@ public static class ServiceCollectionExtensions
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions); return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
}); });
if (!_defaultRegistered)
{
services.AddSingleton<IJwtTokenManagerService>(sp =>
sp.GetRequiredKeyedService<IJwtTokenManagerService>(name));
_defaultRegistered = true;
}
return services; return services;
} }
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
public static IServiceCollection AddJwtTokenManager(
this IServiceCollection services,
IConfiguration configuration,
string sectionName,
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
{
return AddJwtTokenManager(services, configuration, sectionName, null, configureBuilderOptions);
}
} }