public DefaultDataProtectionProvider(
            [NotNull] IOptions <DataProtectionOptions> optionsAccessor,
            [NotNull] IKeyManager keyManager)
        {
            KeyRingBasedDataProtectionProvider rootProvider = new KeyRingBasedDataProtectionProvider(new KeyRingProvider(keyManager));
            var options = optionsAccessor.Options;

            _innerProvider = (!String.IsNullOrEmpty(options.ApplicationDiscriminator))
                ? (IDataProtectionProvider)rootProvider.CreateProtector(options.ApplicationDiscriminator)
                : rootProvider;
        }
示例#2
0
        private static void AddDataProtectionServices(
            IServiceCollection services
            )
        {
            if (OSVersionUtil.IsWindows())
            {
                services.TryAddSingleton <IRegistryPolicyResolver, RegistryPolicyResolver>();
            }

            services.TryAddEnumerable(
                ServiceDescriptor.Singleton <IConfigureOptions <KeyManagementOptions>, KeyManagementOptionsSetup>());
            services.TryAddEnumerable(
                ServiceDescriptor.Transient <IConfigureOptions <DataProtectionOptions>, DataProtectionOptionsSetup>());

            services.TryAddSingleton <IKeyManager, XmlKeyManager>();
            services.TryAddSingleton <IApplicationDiscriminator, HostingApplicationDiscriminator>();
            services.TryAddEnumerable(ServiceDescriptor.Singleton <IStartupFilter, DataProtectionStartupFilter>());

            // Internal services
            services.TryAddSingleton <IDefaultKeyResolver, DefaultKeyResolver>();
            services.TryAddSingleton <IKeyRingProvider, KeyRingProvider>();

            services.TryAddSingleton <IDataProtectionProvider>(s =>
            {
                var dpOptions       = s.GetRequiredService <IOptions <DataProtectionOptions> >();
                var keyRingProvider = s.GetRequiredService <IKeyRingProvider>();
                var loggerFactory   = s.GetService <ILoggerFactory>() ?? NullLoggerFactory.Instance;

                IDataProtectionProvider 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 <ICertificateResolver, CertificateResolver>();
        }