/// <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();
        }
        public void AntiforgeryOptionsSetup_SetsDefaultCookieName_BasedOnApplicationId(
            string applicationId,
            string expectedCookieName)
        {
            // Arrange
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddAntiforgery();
            serviceCollection.ConfigureDataProtection(o => o.SetApplicationName(applicationId));

            var services = serviceCollection.BuildServiceProvider();
            var options = services.GetRequiredService<IOptions<AntiforgeryOptions>>();

            // Act
            var cookieName = options.Options.CookieName;

            // Assert
            Assert.Equal(expectedCookieName, cookieName);
        }
        public void AntiforgeryOptionsSetup_UserOptionsSetup_CanSetCookieName()
        {
            // Arrange
            var serviceCollection = new ServiceCollection();
            serviceCollection.Configure<AntiforgeryOptions>(o =>
            {
                Assert.Null(o.CookieName);
                o.CookieName = "antiforgery";
            });
            serviceCollection.AddAntiforgery();
            serviceCollection.ConfigureDataProtection(o => o.SetApplicationName("HelloWorldApp"));

            var services = serviceCollection.BuildServiceProvider();
            var options = services.GetRequiredService<IOptions<AntiforgeryOptions>>();

            // Act
            var cookieName = options.Value.CookieName;

            // Assert
            Assert.Equal("antiforgery", cookieName);
        }