コード例 #1
0
        public static IServiceCollection AddMiddlerServices(this IServiceCollection serviceCollection, string provider, string connectionString)
        {
            serviceCollection.AddScoped <EndpointRuleRepository>();



            serviceCollection.AddScoped <IVariablesRepository, VariablesRepository>();

            //idpConfiguration ??= new IdpConfiguration();
            //services.AddSingleton(idpConfiguration);

            var _provider = provider?.ToLower();

            switch (_provider)
            {
            case "sqlite":
            {
                SqliteServiceBuilder.AddCoreDbContext(serviceCollection, connectionString);
                break;
            }

            case "sqlserver":
            {
                SqlServerServiceBuilder.AddCoreDbContext(serviceCollection, connectionString);
                break;
            }

            case "postgres":
            {
                PostgresServiceBuilder.AddCoreDbContext(serviceCollection, connectionString);
                break;
            }

            default:
            {
                throw new NotSupportedException($"Database Provider '{provider}' is not supported!");
            }
            }

            serviceCollection.AddMiddlerRepo <EFCoreMiddlerRepository>(ServiceLifetime.Scoped);

            return(serviceCollection);
        }
コード例 #2
0
        public IDPDbContext CreateDbContext(string[] args)
        {
            var serviceCollection = new ServiceCollection();

#if SQLITE
            SqliteServiceBuilder.AddCoreDbContext(serviceCollection, "Data Source=file.db");
#endif

#if SQLSERVER
            SqlServerServiceBuilder.AddCoreDbContext(serviceCollection, "Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = MiddlerApp");
#endif

#if POSTGRES
            PostgresServiceBuilder.AddCoreDbContext(serviceCollection, "Host=10.0.0.22;Database=MidlerApp;Username=postgres;Password=postgres");
#endif

            //serviceCollection.AddCoreDbContextSqlServer("Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = MiddlerApp");

            var sp = serviceCollection.BuildServiceProvider();

            return(sp.GetRequiredService <IDPDbContext>());
        }
コード例 #3
0
        public static void AddMiddlerIdentityServer(this IServiceCollection services, string provider, string connectionstring, IdpConfiguration idpConfiguration = null)
        {
            idpConfiguration ??= new IdpConfiguration();
            services.AddSingleton(idpConfiguration);

            var _provider = provider?.ToLower();

            switch (_provider)
            {
            case "sqlite":
            {
                SqliteServiceBuilder.AddCoreDbContext(services, connectionstring);
                break;
            }

            case "sqlserver":
            {
                SqlServerServiceBuilder.AddCoreDbContext(services, connectionstring);
                break;
            }

            case "postgres":
            {
                PostgresServiceBuilder.AddCoreDbContext(services, connectionstring);
                break;
            }

            default:
            {
                throw new NotSupportedException($"Database Provider '{provider}' is not supported!");
            }
            }

            services.AddSingleton <DataEventDispatcher>();

            services.AddScoped <IPasswordHasher <MUser>, PasswordHasher <MUser> >();
            services.AddScoped <ILocalUserService, LocalUserService>();
            services.AddScoped <IClientService, ClientService>();
            services.AddScoped <IRolesService, RolesService>();
            services.AddScoped <IUsersService, UsersService>();
            services.AddScoped <IApiResourcesService, ApiResourcesService>();
            services.AddScoped <IIdentityResourcesService, IdentityResourcesService>();
            services.AddScoped <IApiScopesService, ApiScopesService>();
            services.AddScoped <IAuthenticationProviderService, AuthenticationProviderService>();


            //services.AddScoped<IAuthorizationCodeStore, AuthorizationCodeStore>();
            services.AddScoped <IUserConsentStore, UserConsentStore>();

            services.AddIdentityServer(options =>
            {
                options.UserInteraction.LoginUrl   = "/login";
                options.UserInteraction.ConsentUrl = "/consent";
                options.UserInteraction.LogoutUrl  = "/logout";
                options.UserInteraction.ErrorUrl   = "/error";

                options.Authentication.CookieAuthenticationScheme = "ids";
            })
            .AddDeveloperSigningCredential()
            .AddCorsPolicyService <MCorsPolicyService>()
            .AddClientStore <ClientStore>()
            .AddDeviceFlowStore <DeviceFlowStore>()
            .AddResourceStore <ResourceStore>()
            .AddPersistedGrantStore <PersistedGrantStore>()
            //.AddClientConfigurationValidator<CustomClientConfigurationValidator>()
            .AddProfileService <LocalUserProfileService>()
            .AddResourceValidator <MResourceValidator>()
            .AddAuthorizeInteractionResponseGenerator <MAuthorizeInteractionResponseGenerator>()
            .AddSecretValidator <CustomHashedSharedSecretValidator>()
            ;


            services.AddTransient <ICorsPolicyService, MCorsPolicyService>();
            services.AddTransient <IUserInfoResponseGenerator, MUserInfoResponseGenerator>();
            services.AddTransient <ICustomTokenValidator, MCustomTokenValidator>();
            services.AddTransient <IApiSecretValidator, CustomApiSecretValidator>();
            services.AddTransient <ISecretsListValidator, CustomSecretValidator>();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Bearer";
                options.DefaultChallengeScheme    = "Bearer";
                options.DefaultSignInScheme       = "ids";
                options.DefaultSignOutScheme      = "ids";
            })
            .AddCookie("ids")
            .AddJwtBearer(o =>
            {
                o.Authority                 = "https://localhost:4445";
                o.RequireHttpsMetadata      = false;
                o.ForwardDefaultSelector    = Selector.ForwardReferenceToken("introspection");
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience = false
                };
            })
            .AddOAuth2Introspection("introspection", options =>
            {
                options.Authority = "https://localhost:4445";

                options.ClientId      = "api";
                options.ClientSecret  = "ABC12abc!";
                options.EnableCaching = true;
                options.CacheDuration = TimeSpan.FromMinutes(10);
                options.Events        = new OAuth2IntrospectionEvents();
                options.Events.OnAuthenticationFailed = async context =>
                {
                    context.HttpContext.Response.Headers["Warning"] = context.Error;
                };
            });
            //.AddNegotiate("Windows", "Windows Authentication", options =>
            //{
            //    options.PersistKerberosCredentials = false;

            //});

            services.AddAuthorization((options) =>
            {
                options.AddPolicy("Admin", policy =>
                {
                    policy.RequireAuthenticatedUser();
                    policy.RequireRole("Administrators");
                });
            });


            services.AddScoped <DefaultResourcesManager>();

            services.AddHostedService <EnsureDefaultResourcesExistsService>();
        }