React to DI changes, AddDataProtection no longer takes Config

This commit is contained in:
Hao Kung 2015-03-04 16:35:33 -08:00
parent bf0f94ce20
commit 32ff156923
2 changed files with 18 additions and 21 deletions

View File

@ -12,23 +12,21 @@ using Microsoft.AspNet.DataProtection.Dpapi;
using Microsoft.AspNet.DataProtection.KeyManagement; using Microsoft.AspNet.DataProtection.KeyManagement;
using Microsoft.AspNet.DataProtection.Repositories; using Microsoft.AspNet.DataProtection.Repositories;
using Microsoft.AspNet.DataProtection.XmlEncryption; using Microsoft.AspNet.DataProtection.XmlEncryption;
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.Framework.DependencyInjection namespace Microsoft.Framework.DependencyInjection
{ {
public static class DataProtectionServiceCollectionExtensions public static class DataProtectionServiceCollectionExtensions
{ {
public static IServiceCollection AddDataProtection(this IServiceCollection services, IConfiguration configuration = null) public static IServiceCollection AddDataProtection(this IServiceCollection services)
{ {
services.AddOptions(configuration); services.AddOptions();
var describe = new ServiceDescriber(configuration);
services.TryAdd(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable() services.TryAdd(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable()
? GetDefaultServicesWindows(describe) ? GetDefaultServicesWindows()
: GetDefaultServicesNonWindows(describe)); : GetDefaultServicesNonWindows());
return services; return services;
} }
private static IEnumerable<IServiceDescriptor> GetDefaultServicesNonWindows(ServiceDescriber describe) private static IEnumerable<ServiceDescriptor> GetDefaultServicesNonWindows()
{ {
// If we're not running on Windows, we can't use CNG. // If we're not running on Windows, we can't use CNG.
@ -36,11 +34,11 @@ namespace Microsoft.Framework.DependencyInjection
// DPAPI routines don't provide authenticity. // DPAPI routines don't provide authenticity.
return new[] return new[]
{ {
describe.Instance<IDataProtectionProvider>(new DpapiDataProtectionProvider(DataProtectionScope.CurrentUser)) ServiceDescriptor.Instance<IDataProtectionProvider>(new DpapiDataProtectionProvider(DataProtectionScope.CurrentUser))
}; };
} }
private static IEnumerable<IServiceDescriptor> GetDefaultServicesWindows(ServiceDescriber describe) private static IEnumerable<ServiceDescriptor> GetDefaultServicesWindows()
{ {
List<ServiceDescriptor> descriptors = new List<ServiceDescriptor>(); List<ServiceDescriptor> descriptors = new List<ServiceDescriptor>();
@ -52,8 +50,8 @@ namespace Microsoft.Framework.DependencyInjection
// cloud DPAPI service comes online. // cloud DPAPI service comes online.
descriptors.AddRange(new[] descriptors.AddRange(new[]
{ {
describe.Singleton<IXmlEncryptor,NullXmlEncryptor>(), ServiceDescriptor.Singleton<IXmlEncryptor,NullXmlEncryptor>(),
describe.Instance<IXmlRepository>(new FileSystemXmlRepository(azureWebSitesKeysFolder)) ServiceDescriptor.Instance<IXmlRepository>(new FileSystemXmlRepository(azureWebSitesKeysFolder))
}); });
} }
else else
@ -64,8 +62,8 @@ namespace Microsoft.Framework.DependencyInjection
{ {
descriptors.AddRange(new[] descriptors.AddRange(new[]
{ {
describe.Instance<IXmlEncryptor>(new DpapiXmlEncryptor(protectToLocalMachine: false)), ServiceDescriptor.Instance<IXmlEncryptor>(new DpapiXmlEncryptor(protectToLocalMachine: false)),
describe.Instance<IXmlRepository>(new FileSystemXmlRepository(localAppDataKeysFolder)) ServiceDescriptor.Instance<IXmlRepository>(new FileSystemXmlRepository(localAppDataKeysFolder))
}); });
} }
else else
@ -80,15 +78,15 @@ namespace Microsoft.Framework.DependencyInjection
// We use same-machine DPAPI since we already know no user profile is loaded. // We use same-machine DPAPI since we already know no user profile is loaded.
descriptors.AddRange(new[] descriptors.AddRange(new[]
{ {
describe.Instance<IXmlEncryptor>(new DpapiXmlEncryptor(protectToLocalMachine: true)), ServiceDescriptor.Instance<IXmlEncryptor>(new DpapiXmlEncryptor(protectToLocalMachine: true)),
describe.Instance<IXmlRepository>(hklmRegXmlRepository) ServiceDescriptor.Instance<IXmlRepository>(hklmRegXmlRepository)
}); });
} }
else else
{ {
// Fall back to DPAPI for now // Fall back to DPAPI for now
return new[] { return new[] {
describe.Instance<IDataProtectionProvider>(new DpapiDataProtectionProvider(DataProtectionScope.LocalMachine)) ServiceDescriptor.Instance<IDataProtectionProvider>(new DpapiDataProtectionProvider(DataProtectionScope.LocalMachine))
}; };
} }
} }
@ -97,10 +95,10 @@ namespace Microsoft.Framework.DependencyInjection
// We use CNG CBC + HMAC by default. // We use CNG CBC + HMAC by default.
descriptors.AddRange(new[] descriptors.AddRange(new[]
{ {
describe.Singleton<IAuthenticatedEncryptorConfigurationFactory, CngCbcAuthenticatedEncryptorConfigurationFactory>(), ServiceDescriptor.Singleton<IAuthenticatedEncryptorConfigurationFactory, CngCbcAuthenticatedEncryptorConfigurationFactory>(),
describe.Singleton<ITypeActivator, TypeActivator>(), ServiceDescriptor.Singleton<ITypeActivator, TypeActivator>(),
describe.Singleton<IKeyManager, XmlKeyManager>(), ServiceDescriptor.Singleton<IKeyManager, XmlKeyManager>(),
describe.Singleton<IDataProtectionProvider, DefaultDataProtectionProvider>() ServiceDescriptor.Singleton<IDataProtectionProvider, DefaultDataProtectionProvider>()
}); });
return descriptors; return descriptors;

View File

@ -4,7 +4,6 @@
using System; using System;
using Microsoft.AspNet.DataProtection.KeyManagement; using Microsoft.AspNet.DataProtection.KeyManagement;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.DataProtection namespace Microsoft.AspNet.DataProtection