Skip to content

Commit e1a65f3

Browse files
committed
feat(DataProtection): add overloads for PersistKeysToStackExchangeRedis to accept IServiceProvider
1 parent bf1f642 commit e1a65f3

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
static Microsoft.AspNetCore.DataProtection.StackExchangeRedisDataProtectionBuilderExtensions.PersistKeysToStackExchangeRedis(this Microsoft.AspNetCore.DataProtection.IDataProtectionBuilder! builder, System.Func<System.IServiceProvider!, StackExchange.Redis.IDatabase!>! databaseFactory) -> Microsoft.AspNetCore.DataProtection.IDataProtectionBuilder!
3+
static Microsoft.AspNetCore.DataProtection.StackExchangeRedisDataProtectionBuilderExtensions.PersistKeysToStackExchangeRedis(this Microsoft.AspNetCore.DataProtection.IDataProtectionBuilder! builder, System.Func<System.IServiceProvider!, StackExchange.Redis.IDatabase!>! databaseFactory, StackExchange.Redis.RedisKey key) -> Microsoft.AspNetCore.DataProtection.IDataProtectionBuilder!

src/DataProtection/StackExchangeRedis/src/RedisDataProtectionBuilderExtensions.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.DataProtection.StackExchangeRedis;
77
using Microsoft.AspNetCore.Shared;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Options;
910
using StackExchange.Redis;
1011

1112
namespace 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

Comments
 (0)