public static void IdentityServerInMemory(this IServiceCollection services) { services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryPersistedGrants() .AddInMemoryIdentityResources(InMemoryConfig.GetIdentityResources()) .AddInMemoryApiResources(InMemoryConfig.GetApiResources()) .AddInMemoryClients(InMemoryConfig.GetClients()) .AddTestUsers(InMemoryConfig.GetUsers()); }
private static void InitDataBase(IServiceProvider service) { using (var scope = service.GetService <IServiceScopeFactory>().CreateScope()) { scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate(); scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>().Database.Migrate(); scope.ServiceProvider.GetRequiredService <ApplicationDbContext>().Database.Migrate(); var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>(); if (!context.Clients.Any()) { foreach (var client in InMemoryConfig.GetClients()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } if (!context.IdentityResources.Any()) { foreach (var resource in InMemoryConfig.GetIdentityResources()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } if (!context.ApiResources.Any()) { foreach (var resource in InMemoryConfig.GetApiResources()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >(); if (!userManager.Users.Any()) { foreach (var testUser in InMemoryConfig.GetUsers()) { var identityUser = new ApplicationUser() { Id = testUser.SubjectId, UserName = testUser.Username }; userManager.CreateAsync(identityUser, testUser.Password).Wait(); userManager.AddClaimsAsync(identityUser, testUser.Claims.ToList()).Wait(); } } } }
private static void InitUserDataBase(IServiceProvider service) { using (var scope = service.GetService <IServiceScopeFactory>().CreateScope()) { scope.ServiceProvider.GetRequiredService <ApplicationDbContext>().Database.Migrate(); var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >(); if (!userManager.Users.Any()) { foreach (var testUser in InMemoryConfig.GetUsers()) { var identityUser = new ApplicationUser() { Id = testUser.SubjectId, UserName = testUser.Username }; userManager.CreateAsync(identityUser, testUser.Password).Wait(); userManager.AddClaimsAsync(identityUser, testUser.Claims.ToList()).Wait(); } } } }