Beispiel #1
0
 public MyAccountController(
     ExtendedIdentityDbContext <User, Role> dbContext,
     ExtendedUserManager <User> userManager,
     IConfiguration configuration,
     IdentityServerApiEndpointsOptions identityServerApiEndpointsOptions,
     IEmailService emailService,
     IPlatformEventService eventService,
     IOptions <GeneralSettings> generalSettings,
     IOptionsSnapshot <IdentityOptions> identityOptions,
     ISmsServiceFactory smsServiceFactory,
     ExtendedConfigurationDbContext configurationDbContext,
     IPersistedGrantStore persistedGrantStore,
     IPersistentGrantSerializer serializer
     )
 {
     _configuration   = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _dbContext       = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
     _emailService    = emailService;
     _eventService    = eventService ?? throw new ArgumentNullException(nameof(eventService));
     _generalSettings = generalSettings?.Value ?? throw new ArgumentNullException(nameof(generalSettings));
     _identityOptions = identityOptions?.Value ?? throw new ArgumentNullException(nameof(identityOptions));
     _identityServerApiEndpointsOptions = identityServerApiEndpointsOptions ?? throw new ArgumentNullException(nameof(identityServerApiEndpointsOptions));
     _smsServiceFactory      = smsServiceFactory ?? throw new ArgumentNullException(nameof(smsServiceFactory));
     _userManager            = userManager ?? throw new ArgumentNullException(nameof(userManager));
     _configurationDbContext = configurationDbContext ?? throw new ArgumentNullException(nameof(configurationDbContext));
     _persistedGrantStore    = persistedGrantStore ?? throw new ArgumentNullException(nameof(persistedGrantStore));
     _serializer             = serializer ?? throw new ArgumentNullException(nameof(serializer));
 }
Beispiel #2
0
        /// <summary>
        /// Registers the <see cref="DbContext"/> to be used by the Identity system.
        /// </summary>
        /// <param name="options">Options for configuring the IdentityServer API feature.</param>
        /// <param name="configureAction">Configuration for <see cref="ExtendedIdentityDbContext{TUser, TRole}"/>.</param>
        public static void AddDbContext(this IdentityServerApiEndpointsOptions options, Action <IdentityDbContextOptions> configureAction)
        {
            var dbContextOptions = new IdentityDbContextOptions();

            configureAction?.Invoke(dbContextOptions);
            options.Services.AddSingleton(dbContextOptions);
            options.Services.AddDbContext <ExtendedIdentityDbContext <User, Role> >(dbContextOptions.ConfigureDbContext);
        }
Beispiel #3
0
 /// <summary>
 /// Creates a new instance of <see cref="ExtendedConfigurationDbContext"/>.
 /// </summary>
 /// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
 /// <param name="storeOptions">Options for configuring the <see cref="ExtendedConfigurationDbContext"/>.</param>
 /// <param name="webHostEnvironment">Provides information about the web hosting environment an application is running in.</param>
 /// <param name="apiOptions">Options for configuring the IdentityServer API feature.</param>
 public ExtendedConfigurationDbContext(
     DbContextOptions <ExtendedConfigurationDbContext> options,
     ConfigurationStoreOptions storeOptions,
     IWebHostEnvironment webHostEnvironment,
     IdentityServerApiEndpointsOptions apiOptions
     ) : base(options, storeOptions)
 {
     if (webHostEnvironment.IsDevelopment() && Database.EnsureCreated())
     {
         this.SeedInitialClaimTypes();
         if (apiOptions.CustomClaims.Any())
         {
             this.SeedCustomClaimTypes(apiOptions.CustomClaims);
         }
     }
 }
Beispiel #4
0
        /// <summary>
        /// Adds the IdentityServer API endpoints in the MVC project.
        /// </summary>
        /// <param name="mvcBuilder">An interface for configuring MVC services.</param>
        /// <param name="configureAction">Configuration options for IdentityServer API feature.</param>
        public static IMvcBuilder AddIdentityServerApiEndpoints(this IMvcBuilder mvcBuilder, Action <IdentityServerApiEndpointsOptions> configureAction = null)
        {
            mvcBuilder.ConfigureApplicationPartManager(x => x.FeatureProviders.Add(new IdentityServerApiFeatureProvider()));
            var services            = mvcBuilder.Services;
            var apiEndpointsOptions = new IdentityServerApiEndpointsOptions {
                Services = services
            };

            // Initialize default options.
            configureAction?.Invoke(apiEndpointsOptions);
            apiEndpointsOptions.Services = null;
            var serviceProvider  = services.BuildServiceProvider();
            var configuration    = serviceProvider.GetRequiredService <IConfiguration>();
            var dbContextOptions = serviceProvider.GetRequiredService <IdentityDbContextOptions>();

            mvcBuilder.AddSettingsApiEndpoints(settingsApiOptions => {
                settingsApiOptions.ApiPrefix          = "api";
                settingsApiOptions.RequiredScope      = IdentityServerApi.Scope;
                settingsApiOptions.ConfigureDbContext = dbContextOptions.ConfigureDbContext;
            });
            services.AddDistributedMemoryCache();
            services.AddFeatureManagement(configuration.GetSection("IdentityServerApiFeatureManagement"));
            // Configure options for CacheResourceFilter.
            services.Configure <CacheResourceFilterOptions>(options => options.DisableCache = apiEndpointsOptions.DisableCache);
            // Invoke action provided by developer to override default options.
            services.AddSingleton(apiEndpointsOptions);
            services.AddGeneralSettings(configuration);
            services.TryAddTransient <IPlatformEventService, PlatformEventService>();
            // Register validation filters.
            services.AddScoped <CreateClaimTypeRequestValidationFilter>();
            services.AddScoped <CreateRoleRequestValidationFilter>();
            // Add authorization policies that are used by the IdentityServer API.
            services.AddIdentityApiAuthorization();
            // Configure antiforgery token options.
            services.Configure <AntiforgeryOptions>(options => options.HeaderName = CustomHeaderNames.AntiforgeryHeaderName);
            services.TryAddScoped <IdentityMessageDescriber>();
            // Try register the extended version of UserManager<User>.
            services.TryAddScoped <ExtendedUserManager <User> >();
            // Register the authentication handler, using a custom scheme name, for local APIs.
            services.AddAuthentication()
            .AddLocalApi(IdentityServerApi.AuthenticationScheme, options => {
                options.ExpectedScope = IdentityServerApi.Scope;
            });
            return(mvcBuilder);
        }
Beispiel #5
0
 /// <summary>
 /// Creates an instance of <see cref="UsersController"/>.
 /// </summary>
 /// <param name="userManager">Provides the APIs for managing user in a persistence store.</param>
 /// <param name="roleManager">Provides the APIs for managing roles in a persistence store.</param>
 /// <param name="dbContext">Class for the Entity Framework database context used for identity.</param>
 /// <param name="persistedGrantService">Implements persisted grant logic.</param>
 /// <param name="clientStore">Retrieval of client configuration.</param>
 /// <param name="apiEndpointsOptions">Options for configuring the IdentityServer API feature.</param>
 /// <param name="eventService">Models the event mechanism used to raise events inside the IdentityServer API.</param>
 /// <param name="generalSettings">General settings for an ASP.NET Core application.</param>
 /// <param name="localizer">Represents an <see cref="IStringLocalizer"/> that provides strings for <see cref="UsersController"/>.</param>
 /// <param name="configurationDbContext">Extended DbContext for the IdentityServer configuration data.</param>
 public UsersController(
     ExtendedUserManager <User> userManager,
     RoleManager <Role> roleManager,
     ExtendedIdentityDbContext <User, Role> dbContext,
     IPersistedGrantService persistedGrantService,
     IClientStore clientStore,
     IdentityServerApiEndpointsOptions apiEndpointsOptions,
     Indice.Services.IPlatformEventService eventService,
     IOptions <GeneralSettings> generalSettings,
     IStringLocalizer <UsersController> localizer,
     ExtendedConfigurationDbContext configurationDbContext
     )
 {
     _userManager            = userManager ?? throw new ArgumentNullException(nameof(userManager));
     _roleManager            = roleManager ?? throw new ArgumentNullException(nameof(roleManager));
     _dbContext              = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
     _persistedGrantService  = persistedGrantService ?? throw new ArgumentNullException(nameof(persistedGrantService));
     _clientStore            = clientStore ?? throw new ArgumentNullException(nameof(clientStore));
     _apiEndpointsOptions    = apiEndpointsOptions ?? throw new ArgumentNullException(nameof(apiEndpointsOptions));
     _eventService           = eventService ?? throw new ArgumentNullException(nameof(eventService));
     _generalSettings        = generalSettings?.Value ?? throw new ArgumentNullException(nameof(generalSettings));
     _localizer              = localizer ?? throw new ArgumentNullException(nameof(localizer));
     _configurationDbContext = configurationDbContext ?? throw new ArgumentNullException(nameof(configurationDbContext));
 }
Beispiel #6
0
 /// <summary>
 /// Creates a new instance of <see cref="ExtendedIdentityDbContext{TUser, TRole}"/>.
 /// </summary>
 /// <param name="dbContextOptions">The options to be used by a <see cref="DbContext"/>.</param>
 /// <param name="options">Options for configuring the IdentityServer API feature.</param>
 /// <param name="webHostEnvironment">Provides information about the web hosting environment an application is running in.</param>
 public ExtendedIdentityDbContext(DbContextOptions <ExtendedIdentityDbContext <TUser, TRole> > dbContextOptions, IdentityServerApiEndpointsOptions options, IWebHostEnvironment webHostEnvironment) : base(dbContextOptions)
 {
     if (webHostEnvironment.IsDevelopment() && Database.EnsureCreated())
     {
         this.SeedAdminUser();
         if (options.SeedDummyUsers)
         {
             this.SeedDummyUsers();
         }
         this.SeedCustomUsers(options.InitialUsers);
     }
 }
Beispiel #7
0
 /// <summary>
 /// Registers an implementation of <see cref="IPlatformEventHandler{TEvent}"/> for the specified event type.
 /// </summary>
 /// <typeparam name="TEvent">The type of the event to handler.</typeparam>
 /// <typeparam name="TEventHandler">The handler to user for the specified event.</typeparam>
 /// <param name="options">Options for configuring the IdentityServer API feature.</param>
 public static IdentityServerApiEndpointsOptions AddPlatformEventHandler <TEvent, TEventHandler>(this IdentityServerApiEndpointsOptions options)
     where TEvent : IPlatformEvent
     where TEventHandler : class, IPlatformEventHandler <TEvent>
 {
     options.Services.AddPlatformEventHandler <TEvent, TEventHandler>();
     return(options);
 }