Ejemplo n.º 1
0
 /// <summary>
 /// This follows the same initialization that is provided when <see cref="IDataProtectionProvider"/>
 /// is initialized within ASP.NET 5.0 Dependency Injection.
 /// </summary>
 /// <returns>A fully initialized <see cref="IDataProtectionProvider"/>.</returns>
 internal static IDataProtectionProvider GetDataProtectionProvider()
 {
     ServiceCollection serviceCollection = new ServiceCollection();
     serviceCollection.AddDataProtection();
     IServiceProvider services = serviceCollection.BuildServiceProvider();
     return services.GetDataProtectionProvider();
 }
        /// <summary>
        /// Creates an <see cref="DataProtectionProvider"/> given a location at which to store keys and an
        /// optional configuration callback.
        /// </summary>
        /// <param name="keyDirectory">The <see cref="DirectoryInfo"/> in which keys should be stored. This may
        /// represent a directory on a local disk or a UNC share.</param>
        /// <param name="configure">An optional callback which provides further configuration of the data protection
        /// system. See <see cref="DataProtectionConfiguration"/> for more information.</param>
        public DataProtectionProvider([NotNull] DirectoryInfo keyDirectory, Action<DataProtectionConfiguration> configure)
        {
            // build the service collection
            ServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddDataProtection();
            serviceCollection.ConfigureDataProtection(configurationObject =>
            {
                configurationObject.PersistKeysToFileSystem(keyDirectory);
                configure?.Invoke(configurationObject);
            });

            // extract the provider instance from the service collection
            _innerProvider = serviceCollection.BuildServiceProvider().GetRequiredService<IDataProtectionProvider>();
        }
        static void Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddDataProtection();
            serviceCollection.ConfigureDataProtection(options =>
            {
                options.SetApplicationName(appName);
            });

            var services = serviceCollection.BuildServiceProvider();

            var demoInstance = ActivatorUtilities.CreateInstance<ProtectUnprotectDemo>(services);
            demoInstance.Demonstrate();
        }
        /// <summary>
        /// Provides a default implementation of required services, calls the developer's
        /// configuration overrides, then creates an <see cref="IDataProtectionProvider"/>.
        /// </summary>
        internal IDataProtectionProvider InternalConfigureServicesAndCreateProtectionProvider()
        {
            // Configure the default implementation, passing in our custom discriminator
            var services = new ServiceCollection();
            services.AddDataProtection();
            services.AddInstance<IApplicationDiscriminator>(new SystemWebApplicationDiscriminator());

            // Run user-specified configuration and get an instance of the provider
            ConfigureServices(services);
            var provider = CreateDataProtectionProvider(services.BuildServiceProvider());
            if (provider == null)
            {
                throw new InvalidOperationException(Resources.Startup_CreateProviderReturnedNull);
            }

            // And we're done!
            return provider;
        }