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.IO;
using System.Security.Cryptography;
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.AspNet.Security.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNet.Security.DataProtection.Cng;
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.XmlEncryption;
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()
{
return GetDefaultServices(new Configuration());
}
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration)
public static IServiceCollection AddDataProtection(this IServiceCollection services, IConfiguration configuration = null)
{
services.AddOptions(configuration);
var describe = new ServiceDescriber(configuration);
List<IServiceDescriptor> descriptors = new List<IServiceDescriptor>();
descriptors.AddRange(OptionsServices.GetDefaultServices(configuration));
descriptors.AddRange(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable()
services.TryAdd(OSVersionUtil.IsBCryptOnWin7OrLaterAvailable()
? GetDefaultServicesWindows(describe)
: GetDefaultServicesNonWindows(describe));
return descriptors;
return services;
}
private static IEnumerable<IServiceDescriptor> GetDefaultServicesNonWindows(ServiceDescriber describe)

View File

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