From 697745c4909886736ce340782b606d2c43f90cc6 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Wed, 5 Apr 2017 12:38:40 -0700 Subject: [PATCH] [Fixes #218] Set IXmlRepository using ConfigureOptions --- .../AzureDataProtectionBuilderExtensions.cs | 11 ++++--- .../RedisDataProtectionBuilderExtensions.cs | 13 +++++--- ...zureDataProtectionBuilderExtensionsTest.cs | 32 +++++++++++++++++++ ...spNetCore.DataProtection.Redis.Test.csproj | 1 + ...edisDataProtectionBuilderExtensionsTest.cs | 32 +++++++++++++++++++ 5 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 test/Microsoft.AspNetCore.DataProtection.AzureStorage.Test/AzureDataProtectionBuilderExtensionsTest.cs create mode 100644 test/Microsoft.AspNetCore.DataProtection.Redis.Test/RedisDataProtectionBuilderExtensionsTest.cs diff --git a/src/Microsoft.AspNetCore.DataProtection.AzureStorage/AzureDataProtectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.DataProtection.AzureStorage/AzureDataProtectionBuilderExtensions.cs index 90f403b..8ff6292 100644 --- a/src/Microsoft.AspNetCore.DataProtection.AzureStorage/AzureDataProtectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.DataProtection.AzureStorage/AzureDataProtectionBuilderExtensions.cs @@ -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 blobRefFactory) + private static IDataProtectionBuilder PersistKeystoAzureBlobStorageInternal(IDataProtectionBuilder builder, Func blobRefFactory) { - config.Services.AddSingleton(services => new AzureBlobXmlRepository(blobRefFactory)); - return config; + builder.Services.Configure(options => + { + options.XmlRepository = new AzureBlobXmlRepository(blobRefFactory); + }); + return builder; } } } diff --git a/src/Microsoft.AspNetCore.DataProtection.Redis/RedisDataProtectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.DataProtection.Redis/RedisDataProtectionBuilderExtensions.cs index f18e053..97593cb 100644 --- a/src/Microsoft.AspNetCore.DataProtection.Redis/RedisDataProtectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.DataProtection.Redis/RedisDataProtectionBuilderExtensions.cs @@ -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 databaseFactory, RedisKey key) + private static IDataProtectionBuilder PersistKeysToRedisInternal(IDataProtectionBuilder builder, Func databaseFactory, RedisKey key) { - config.Services.TryAddSingleton(services => new RedisXmlRepository(databaseFactory, key)); - return config; + builder.Services.Configure(options => + { + options.XmlRepository = new RedisXmlRepository(databaseFactory, key); + }); + return builder; } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.DataProtection.AzureStorage.Test/AzureDataProtectionBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.DataProtection.AzureStorage.Test/AzureDataProtectionBuilderExtensionsTest.cs new file mode 100644 index 0000000..d386352 --- /dev/null +++ b/test/Microsoft.AspNetCore.DataProtection.AzureStorage.Test/AzureDataProtectionBuilderExtensionsTest.cs @@ -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>(); + Assert.IsType(options.Value.XmlRepository); + } + } +} diff --git a/test/Microsoft.AspNetCore.DataProtection.Redis.Test/Microsoft.AspNetCore.DataProtection.Redis.Test.csproj b/test/Microsoft.AspNetCore.DataProtection.Redis.Test/Microsoft.AspNetCore.DataProtection.Redis.Test.csproj index 8f86aaf..0c82d3e 100644 --- a/test/Microsoft.AspNetCore.DataProtection.Redis.Test/Microsoft.AspNetCore.DataProtection.Redis.Test.csproj +++ b/test/Microsoft.AspNetCore.DataProtection.Redis.Test/Microsoft.AspNetCore.DataProtection.Redis.Test.csproj @@ -15,6 +15,7 @@ + diff --git a/test/Microsoft.AspNetCore.DataProtection.Redis.Test/RedisDataProtectionBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.DataProtection.Redis.Test/RedisDataProtectionBuilderExtensionsTest.cs new file mode 100644 index 0000000..a3d8f82 --- /dev/null +++ b/test/Microsoft.AspNetCore.DataProtection.Redis.Test/RedisDataProtectionBuilderExtensionsTest.cs @@ -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(); + var serviceCollection = new ServiceCollection(); + var builder = serviceCollection.AddDataProtection(); + + // Act + builder.PersistKeysToRedis(connection); + var services = serviceCollection.BuildServiceProvider(); + + // Assert + var options = services.GetRequiredService>(); + Assert.IsType(options.Value.XmlRepository); + } + } +}