// 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.Cryptography.Cng;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.Infrastructure;
using Microsoft.AspNetCore.DataProtection.Internal;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
using Microsoft.AspNetCore.DataProtection.XmlEncryption;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.Extensions.DependencyInjection
{
///
/// Extension methods for setting up data protection services in an .
///
public static class DataProtectionServiceCollectionExtensions
{
///
/// Adds data protection services to the specified .
///
/// The to add services to.
public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.TryAddSingleton();
services.AddOptions();
AddDataProtectionServices(services);
return new DataProtectionBuilder(services);
}
///
/// Adds data protection services to the specified .
///
/// The to add services to.
/// An to configure the provided .
/// A reference to this instance after the operation has completed.
public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services, Action setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
var builder = services.AddDataProtection();
services.Configure(setupAction);
return builder;
}
private static void AddDataProtectionServices(IServiceCollection services)
{
services.TryAddSingleton(DataProtectionProviderFactory.GetDefaultLoggerFactory());
if (OSVersionUtil.IsWindows())
{
services.TryAddSingleton();
}
services.TryAddEnumerable(
ServiceDescriptor.Singleton, KeyManagementOptionsSetup>());
services.TryAddEnumerable(
ServiceDescriptor.Transient, DataProtectionOptionsSetup>());
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddEnumerable(ServiceDescriptor.Singleton());
// Internal services
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton(s =>
{
var dpOptions = s.GetRequiredService>();
var keyRingProvider = s.GetRequiredService();
var loggerFactory = s.GetRequiredService();
IDataProtectionProvider dataProtectionProvider = null;
dataProtectionProvider = new KeyRingBasedDataProtectionProvider(keyRingProvider, loggerFactory);
// Link the provider to the supplied discriminator
if (!string.IsNullOrEmpty(dpOptions.Value.ApplicationDiscriminator))
{
dataProtectionProvider = dataProtectionProvider.CreateProtector(dpOptions.Value.ApplicationDiscriminator);
}
return dataProtectionProvider;
});
services.TryAddSingleton();
}
}
}