コード例 #1
0
        public Startup(IHostingEnvironment env)
        {
            // Load all the configuration information from the "json" file & the environment variables.
            var builder = new ConfigurationBuilder()
                          .AddJsonFile("appsettings.json")
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                          .AddEnvironmentVariables();

            Configuration          = builder.Build();
            _authenticationOptions = new AuthenticationMiddlewareOptions
            {
                IdServer = new IdServerOptions
                {
                    ExternalLoginCallback = "/Authenticate/LoginCallback",
                    LoginUrls             = new List <string>
                    {
                        "/Authenticate",
                        "/Authenticate/ExternalLogin",
                        "/Authenticate/OpenId",
                        "/Authenticate/LocalLoginOpenId",
                        "/Authenticate/LocalLogin",
                        "/Authenticate/ExternalLoginOpenId"
                    }
                },
                ConfigurationEdp = new ConfigurationEdpOptions
                {
                    ConfigurationUrl = Configuration["ConfigurationEdp:Url"],
                    ClientId         = Configuration["ConfigurationEdp:ClientId"],
                    ClientSecret     = Configuration["ConfigurationEdp:ClientSecret"],
                    Scopes           = new List <string>
                    {
                        "display_configuration"
                    }
                }
            };
            var twoFactorServiceStore = new TwoFactorServiceStore();
            var factory = new SimpleIdServerConfigurationClientFactory();

            twoFactorServiceStore.Add(new DefaultTwilioSmsService(factory, Configuration["ConfigurationEdp:Url"]));
            twoFactorServiceStore.Add(new DefaultEmailService(factory, Configuration["ConfigurationEdp:Url"]));
            _options = new IdentityServerOptions
            {
                IsDeveloperModeEnabled = false,
                DataSource             = new DataSourceOptions
                {
                    IsOpenIdDataMigrated   = true,
                    IsEvtStoreDataMigrated = true,
                },
                Logging = new LoggingOptions
                {
                    ElasticsearchOptions = new ElasticsearchOptions(),
                    FileLogOptions       = new FileLogOptions()
                },
                Authenticate = new AuthenticateOptions
                {
                    CookieName = Constants.CookieName
                },
                Scim = new ScimOptions
                {
                    IsEnabled = true,
                    EndPoint  = "http://localhost:5555/"
                },
                TwoFactorServiceStore = twoFactorServiceStore
            };

            var openIdType   = Configuration["Db:OpenIdType"];
            var evtStoreType = Configuration["Db:EvtStoreType"];

            if (string.Equals(openIdType, "SQLSERVER", System.StringComparison.CurrentCultureIgnoreCase))
            {
                _options.DataSource.OpenIdDataSourceType   = DataSourceTypes.SqlServer;
                _options.DataSource.OpenIdConnectionString = Configuration["Db:OpenIdConnectionString"];
            }
            else if (string.Equals(openIdType, "SQLITE", System.StringComparison.CurrentCultureIgnoreCase))
            {
                _options.DataSource.OpenIdDataSourceType   = DataSourceTypes.SqlLite;
                _options.DataSource.OpenIdConnectionString = Configuration["Db:OpenIdConnectionString"];
            }
            else if (string.Equals(openIdType, "POSTGRE", System.StringComparison.CurrentCultureIgnoreCase))
            {
                _options.DataSource.OpenIdDataSourceType   = DataSourceTypes.Postgre;
                _options.DataSource.OpenIdConnectionString = Configuration["Db:OpenIdConnectionString"];
            }
            else
            {
                _options.DataSource.OpenIdDataSourceType = DataSourceTypes.InMemory;
            }

            if (string.Equals(evtStoreType, "SQLSERVER", System.StringComparison.CurrentCultureIgnoreCase))
            {
                _options.DataSource.EvtStoreDataSourceType   = DataSourceTypes.SqlServer;
                _options.DataSource.EvtStoreConnectionString = Configuration["Db:EvtStoreConnectionString"];
            }
            else if (string.Equals(evtStoreType, "SQLITE", System.StringComparison.CurrentCultureIgnoreCase))
            {
                _options.DataSource.EvtStoreDataSourceType   = DataSourceTypes.SqlLite;
                _options.DataSource.EvtStoreConnectionString = Configuration["Db:EvtStoreConnectionString"];
            }
            else if (string.Equals(evtStoreType, "POSTGRE", System.StringComparison.CurrentCultureIgnoreCase))
            {
                _options.DataSource.EvtStoreDataSourceType   = DataSourceTypes.Postgre;
                _options.DataSource.EvtStoreConnectionString = Configuration["Db:EvtStoreConnectionString"];
            }
            else
            {
                _options.DataSource.EvtStoreDataSourceType = DataSourceTypes.InMemory;
            }

            bool isLogFileEnabled,
                 isElasticSearchEnabled;

            if (bool.TryParse(Configuration["Log:File:Enabled"], out isLogFileEnabled))
            {
                _options.Logging.FileLogOptions.IsEnabled = isLogFileEnabled;
                if (isLogFileEnabled)
                {
                    _options.Logging.FileLogOptions.PathFormat = Configuration["Log:File:PathFormat"];
                }
            }

            if (bool.TryParse(Configuration["Log:Elasticsearch:Enabled"], out isElasticSearchEnabled))
            {
                _options.Logging.ElasticsearchOptions.IsEnabled = isElasticSearchEnabled;
                if (isElasticSearchEnabled)
                {
                    _options.Logging.ElasticsearchOptions.Url = Configuration["Log:Elasticsearch:Url"];
                }
            }
        }
コード例 #2
0
        public static IServiceCollection AddHostIdentityServer(this IServiceCollection serviceCollection, IdentityServerOptions options)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.AuthenticateResourceOwner == null)
            {
                serviceCollection.AddTransient <IAuthenticateResourceOwnerService, DefaultAuthenticateResourceOwerService>();
            }
            else
            {
                serviceCollection.AddTransient(typeof(IAuthenticateResourceOwnerService), options.AuthenticateResourceOwner);
            }

            if (options.ConfigurationService == null)
            {
                serviceCollection.AddTransient <IConfigurationService, DefaultConfigurationService>();
            }
            else
            {
                serviceCollection.AddTransient(typeof(IConfigurationService), options.ConfigurationService);
            }

            if (options.PasswordService == null)
            {
                serviceCollection.AddTransient <IPasswordService, DefaultPasswordService>();
            }
            else
            {
                serviceCollection.AddTransient(typeof(IPasswordService), options.PasswordService);
            }

            var twoFactorServiceStore = new TwoFactorServiceStore();

            if (options.TwoFactorAuthentications != null)
            {
                foreach (var twoFactorAuthentication in options.TwoFactorAuthentications)
                {
                    if (twoFactorAuthentication.TwoFactorAuthType != Core.Common.Models.TwoFactorAuthentications.NONE && twoFactorAuthentication.TwoFactorAuthenticationService != null)
                    {
                        twoFactorServiceStore.Add(twoFactorAuthentication.TwoFactorAuthenticationService);
                    }
                }
            }

            serviceCollection.AddSingleton <ITwoFactorServiceStore>(twoFactorServiceStore);
            serviceCollection
            .AddSingleton(options.Authenticate)
            .AddSingleton(options.Scim)
            .AddTransient <IRedirectInstructionParser, RedirectInstructionParser>()
            .AddTransient <IActionResultParser, ActionResultParser>()
            .AddSingleton <IHttpContextAccessor, HttpContextAccessor>()
            .AddSingleton <IActionContextAccessor, ActionContextAccessor>()
            .AddDataProtection();
            return(serviceCollection);
        }