[Fixes #218] Set IXmlRepository using ConfigureOptions
This commit is contained in:
Родитель
9b5a26f774
Коммит
697745c490
|
@ -3,7 +3,7 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.AspNetCore.DataProtection.AzureStorage;
|
||||
using Microsoft.AspNetCore.DataProtection.Repositories;
|
||||
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.WindowsAzure.Storage.Auth;
|
||||
|
@ -163,10 +163,13 @@ namespace Microsoft.AspNetCore.DataProtection
|
|||
}
|
||||
|
||||
// important: the Func passed into this method must return a new instance with each call
|
||||
private static IDataProtectionBuilder PersistKeystoAzureBlobStorageInternal(IDataProtectionBuilder config, Func<CloudBlockBlob> blobRefFactory)
|
||||
private static IDataProtectionBuilder PersistKeystoAzureBlobStorageInternal(IDataProtectionBuilder builder, Func<CloudBlockBlob> blobRefFactory)
|
||||
{
|
||||
config.Services.AddSingleton<IXmlRepository>(services => new AzureBlobXmlRepository(blobRefFactory));
|
||||
return config;
|
||||
builder.Services.Configure<KeyManagementOptions>(options =>
|
||||
{
|
||||
options.XmlRepository = new AzureBlobXmlRepository(blobRefFactory);
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
using System;
|
||||
using StackExchange.Redis;
|
||||
using Microsoft.AspNetCore.DataProtection.Repositories;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.AspNetCore.DataProtection
|
||||
{
|
||||
|
@ -66,10 +66,13 @@ namespace Microsoft.AspNetCore.DataProtection
|
|||
return PersistKeysToRedisInternal(builder, () => connectionMultiplexer.GetDatabase(), key);
|
||||
}
|
||||
|
||||
private static IDataProtectionBuilder PersistKeysToRedisInternal(IDataProtectionBuilder config, Func<IDatabase> databaseFactory, RedisKey key)
|
||||
private static IDataProtectionBuilder PersistKeysToRedisInternal(IDataProtectionBuilder builder, Func<IDatabase> databaseFactory, RedisKey key)
|
||||
{
|
||||
config.Services.TryAddSingleton<IXmlRepository>(services => new RedisXmlRepository(databaseFactory, key));
|
||||
return config;
|
||||
builder.Services.Configure<KeyManagementOptions>(options =>
|
||||
{
|
||||
options.XmlRepository = new RedisXmlRepository(databaseFactory, key);
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.WindowsAzure.Storage.Blob;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.DataProtection.AzureStorage
|
||||
{
|
||||
public class AzureDataProtectionBuilderExtensionsTest
|
||||
{
|
||||
[Fact]
|
||||
public void PersistKeysToAzureBlobStorage_UsesAzureBlobXmlRepository()
|
||||
{
|
||||
// Arrange
|
||||
var container = new CloudBlobContainer(new Uri("http://www.example.com"));
|
||||
var serviceCollection = new ServiceCollection();
|
||||
var builder = serviceCollection.AddDataProtection();
|
||||
|
||||
// Act
|
||||
builder.PersistKeysToAzureBlobStorage(container, "keys.xml");
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
var options = services.GetRequiredService<IOptions<KeyManagementOptions>>();
|
||||
Assert.IsType<AzureBlobXmlRepository>(options.Value.XmlRepository);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.DataProtection.Abstractions\Microsoft.AspNetCore.DataProtection.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.DataProtection.Redis\Microsoft.AspNetCore.DataProtection.Redis.csproj" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
|
||||
<PackageReference Include="Moq" Version="$(MoqVersion)" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using StackExchange.Redis;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.DataProtection.Redis
|
||||
{
|
||||
public class RedisDataProtectionBuilderExtensionsTest
|
||||
{
|
||||
[Fact]
|
||||
public void PersistKeysToRedis_UsesRedisXmlRepository()
|
||||
{
|
||||
// Arrange
|
||||
var connection = Mock.Of<IConnectionMultiplexer>();
|
||||
var serviceCollection = new ServiceCollection();
|
||||
var builder = serviceCollection.AddDataProtection();
|
||||
|
||||
// Act
|
||||
builder.PersistKeysToRedis(connection);
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
var options = services.GetRequiredService<IOptions<KeyManagementOptions>>();
|
||||
Assert.IsType<RedisXmlRepository>(options.Value.XmlRepository);
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче