66using Microsoft . AspNetCore . DataProtection . StackExchangeRedis ;
77using Microsoft . AspNetCore . Shared ;
88using Microsoft . Extensions . DependencyInjection ;
9+ using Microsoft . Extensions . Options ;
910using StackExchange . Redis ;
1011
1112namespace Microsoft . AspNetCore . DataProtection ;
@@ -28,7 +29,7 @@ public static IDataProtectionBuilder PersistKeysToStackExchangeRedis(this IDataP
2829 {
2930 ArgumentNullThrowHelper . ThrowIfNull ( builder ) ;
3031 ArgumentNullThrowHelper . ThrowIfNull ( databaseFactory ) ;
31- return PersistKeysToStackExchangeRedisInternal ( builder , databaseFactory , key ) ;
32+ return PersistKeysToStackExchangeRedisInternal ( builder , _ => databaseFactory ( ) , key ) ;
3233 }
3334
3435 /// <summary>
@@ -53,15 +54,44 @@ public static IDataProtectionBuilder PersistKeysToStackExchangeRedis(this IDataP
5354 {
5455 ArgumentNullThrowHelper . ThrowIfNull ( builder ) ;
5556 ArgumentNullThrowHelper . ThrowIfNull ( connectionMultiplexer ) ;
56- return PersistKeysToStackExchangeRedisInternal ( builder , ( ) => connectionMultiplexer . GetDatabase ( ) , key ) ;
57+ return PersistKeysToStackExchangeRedisInternal ( builder , _ => connectionMultiplexer . GetDatabase ( ) , key ) ;
58+ }
59+
60+ /// <summary>
61+ /// Configures the data protection system to persist keys to the default key ('DataProtection-Keys') in Redis database
62+ /// </summary>
63+ /// <param name="builder">The builder instance to modify.</param>
64+ /// <param name="databaseFactory">The <see cref="IConnectionMultiplexer"/> for database access.</param>
65+ /// <returns>A reference to the <see cref="IDataProtectionBuilder" /> after this operation has completed.</returns>
66+ public static IDataProtectionBuilder PersistKeysToStackExchangeRedis ( this IDataProtectionBuilder builder , Func < IServiceProvider , IDatabase > databaseFactory )
67+ {
68+ return PersistKeysToStackExchangeRedis ( builder , databaseFactory , DataProtectionKeysName ) ;
69+ }
70+
71+ /// <summary>
72+ /// Configures the data protection system to persist keys to the specified key in Redis database
73+ /// </summary>
74+ /// <param name="builder">The builder instance to modify.</param>
75+ /// <param name="databaseFactory">The <see cref="IConnectionMultiplexer"/> for database access.</param>
76+ /// <param name="key">The <see cref="RedisKey"/> used to store key list.</param>
77+ /// <returns>A reference to the <see cref="IDataProtectionBuilder" /> after this operation has completed.</returns>
78+ public static IDataProtectionBuilder PersistKeysToStackExchangeRedis ( this IDataProtectionBuilder builder , Func < IServiceProvider , IDatabase > databaseFactory , RedisKey key )
79+ {
80+ ArgumentNullThrowHelper . ThrowIfNull ( builder ) ;
81+ ArgumentNullThrowHelper . ThrowIfNull ( databaseFactory ) ;
82+ return PersistKeysToStackExchangeRedisInternal ( builder , databaseFactory , key ) ;
5783 }
5884
59- private static IDataProtectionBuilder PersistKeysToStackExchangeRedisInternal ( IDataProtectionBuilder builder , Func < IDatabase > databaseFactory , RedisKey key )
85+ private static IDataProtectionBuilder PersistKeysToStackExchangeRedisInternal ( IDataProtectionBuilder builder , Func < IServiceProvider , IDatabase > databaseFactory , RedisKey key )
6086 {
61- builder . Services . Configure < KeyManagementOptions > ( options =>
87+ builder . Services . AddSingleton < IConfigureOptions < KeyManagementOptions > > ( services =>
6288 {
63- options . XmlRepository = new RedisXmlRepository ( databaseFactory , key ) ;
89+ return new ConfigureOptions < KeyManagementOptions > ( options =>
90+ {
91+ options . XmlRepository = new RedisXmlRepository ( ( ) => databaseFactory ( services ) , key ) ;
92+ } ) ;
6493 } ) ;
94+
6595 return builder ;
6696 }
6797}
0 commit comments