public EnsureDefaultResourcesExistsService(
     ILogger <EnsureDefaultResourcesExistsService> logger,
     IHostApplicationLifetime appLifetime,
     IServiceProvider provider,
     IdpConfiguration idpConfiguration)
 {
     _logger           = logger;
     _appLifetime      = appLifetime;
     _provider         = provider;
     _idpConfiguration = idpConfiguration;
 }
Beispiel #2
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>();
        }
Beispiel #3
0
 public DefaultResourcesManager(IDPDbContext dbContext, IdpConfiguration idpConfiguration)
 {
     _idpConfiguration = idpConfiguration;
     DbContext         = dbContext;
 }