Exemplo n.º 1
0
        /// <summary>
        /// Configures NHibernate-based database support for IdentityServer.
        /// - Adds NHibernate implementation of IClientStore, IResourceStore, and ICorsPolicyService (configuration store)
        /// - Adds NHibernate implementation of IPersistedGrantStore, IDeviceFlowStore and TokenCleanupService (operational store).
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="databaseConfiguration">The NHibernate configuration to access the underlying database.</param>
        /// <param name="configurationStoreOptionsAction">The configurations store options action.</param>
        /// <param name="operationalStoreOptionsAction">The operational store options action.</param>
        /// <param name="createDatabaseSchema">If true, drops and creates the database schema (only the schema, NOT the database). Default is false.</param>
        public static IIdentityServerBuilder AddNHibernateStores(
            this IIdentityServerBuilder builder,
            NHibernate.Cfg.Configuration databaseConfiguration,
            Action <ConfigurationStoreOptions> configurationStoreOptionsAction,
            Action <OperationalStoreOptions> operationalStoreOptionsAction,
            bool createDatabaseSchema = false)
        {
            var configStoreOptions = new ConfigurationStoreOptions();

            builder.Services.AddSingleton(configStoreOptions);
            configurationStoreOptionsAction?.Invoke(configStoreOptions);

            var operationalStoreOptions = new OperationalStoreOptions();

            builder.Services.AddSingleton(operationalStoreOptions);
            operationalStoreOptionsAction?.Invoke(operationalStoreOptions);

            builder.AddNHibernatePersistenceSupport(databaseConfiguration, configStoreOptions, operationalStoreOptions);

            // Creates database schema from scratch, if requested;
            if (createDatabaseSchema)
            {
                CreateDatabaseSchema(databaseConfiguration);
            }

            // Adds configuration store components
            if (configStoreOptions.EnableConfigurationStoreCache)
            {
                builder.AddCachedConfigurationStore();
            }
            else
            {
                builder.AddConfigurationStore();
            }

            // Adds operational store components.
            builder.AddOperationalStore();

            return(builder);
        }