private static void EnsureSeedData(ConfigurationDbContext context, UserManager <ApplicationUser> userManager) { if (!context.Clients.Any()) { foreach (var client in IdentityConfig.GetClients().ToList()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); // foreach (var user in IdentityConfig.GetUsers()) { userManager.CreateAsync(new ApplicationUser { UserName = user.Username }, user.Password); } } if (!context.Scopes.Any()) { foreach (var client in IdentityConfig.GetScopes().ToList()) { context.Scopes.Add(client.ToEntity()); } context.SaveChanges(); } }
public void InitializeDatabase(IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate(); var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>(); context.Database.Migrate(); if (!context.Clients.Any()) { foreach (var client in IdentityConfig.GetClients()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } if (!context.IdentityResources.Any()) { foreach (var resource in IdentityConfig.GetIdentityResources()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } if (!context.ApiResources.Any()) { foreach (var resource in IdentityConfig.GetApiResources()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } if (!context.ApiScopes.Any()) { foreach (var resource in IdentityConfig.GetScopes()) { context.ApiScopes.Add(resource.ToEntity()); } context.SaveChanges(); } } }
public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddNewtonsoftJson(); services.AddHttpContextAccessor(); services.AddIdentityServer() .AddInMemoryApiResources(IdentityConfig.GetApiResources()) .AddInMemoryIdentityResources(IdentityConfig.GetIdentityResources()) .AddInMemoryClients(IdentityConfig.GetClients()) .AddInMemoryApiScopes(IdentityConfig.GetScopes()) .AddDeveloperSigningCredential(false) .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>() .AddProfileService <CustomProfileService>(); services.AddLogging(builder => { builder.AddConsole(); }); services.AddScoped <ITokenProvider, TokenProvider>(); }