GetServices -> AddDataProtection

This commit is contained in:
Hao Kung 2014-11-24 16:40:13 -08:00
parent 8db4a11819
commit f3e9381f16
2 changed files with 8 additions and 19 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.AspNet.Security.DataProtection.AuthenticatedEncryption; using Microsoft.AspNet.Security.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNet.Security.DataProtection.Cng; using Microsoft.AspNet.Security.DataProtection.Cng;
using Microsoft.AspNet.Security.DataProtection.Dpapi; using Microsoft.AspNet.Security.DataProtection.Dpapi;
@ -12,28 +13,19 @@ using Microsoft.AspNet.Security.DataProtection.KeyManagement;
using Microsoft.AspNet.Security.DataProtection.Repositories; using Microsoft.AspNet.Security.DataProtection.Repositories;
using Microsoft.AspNet.Security.DataProtection.XmlEncryption; using Microsoft.AspNet.Security.DataProtection.XmlEncryption;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Security.DataProtection namespace Microsoft.Framework.DependencyInjection
{ {
public static class DataProtectionServices public static class DataProtectionServiceCollectionExtensions
{ {
public static IEnumerable<IServiceDescriptor> GetDefaultServices() public static IServiceCollection AddDataProtection(this IServiceCollection services, IConfiguration configuration = null)
{
return GetDefaultServices(new Configuration());
}
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration)
{ {
services.AddOptions(configuration);
var describe = new ServiceDescriber(configuration); var describe = new ServiceDescriber(configuration);
services.TryAdd(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable()
List<IServiceDescriptor> descriptors = new List<IServiceDescriptor>();
descriptors.AddRange(OptionsServices.GetDefaultServices(configuration));
descriptors.AddRange(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable()
? GetDefaultServicesWindows(describe) ? GetDefaultServicesWindows(describe)
: GetDefaultServicesNonWindows(describe)); : GetDefaultServicesNonWindows(describe));
return descriptors; return services;
} }
private static IEnumerable<IServiceDescriptor> GetDefaultServicesNonWindows(ServiceDescriber describe) private static IEnumerable<IServiceDescriptor> GetDefaultServicesNonWindows(ServiceDescriber describe)

View File

@ -16,10 +16,7 @@ namespace Microsoft.AspNet.Security.DataProtection
public DefaultDataProtectionProvider() public DefaultDataProtectionProvider()
{ {
// use DI defaults // use DI defaults
var collection = new ServiceCollection(); var serviceProvider = new ServiceCollection().AddDataProtection().BuildServiceProvider();
var defaultServices = DataProtectionServices.GetDefaultServices();
collection.Add(defaultServices);
var serviceProvider = collection.BuildServiceProvider();
_innerProvider = serviceProvider.GetRequiredService<IDataProtectionProvider>(); _innerProvider = serviceProvider.GetRequiredService<IDataProtectionProvider>();
} }