Created
December 11, 2018 08:43
-
-
Save ngohungphuc/26cf3cea07c7b7f577ed251fe0d0be83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void InitializeDbTestData(IApplicationBuilder app) | |
{ | |
using (var scope = app.ApplicationServices.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 Clients.Get()) | |
{ | |
context.Clients.Add(client.ToEntity()); | |
} | |
context.SaveChanges(); | |
} | |
if (!context.IdentityResources.Any()) | |
{ | |
foreach (var resource in Resources.GetIdentityResources()) | |
{ | |
context.IdentityResources.Add(resource.ToEntity()); | |
} | |
context.SaveChanges(); | |
} | |
if (!context.ApiResources.Any()) | |
{ | |
foreach (var resource in Resources.GetApiResources()) | |
{ | |
context.ApiResources.Add(resource.ToEntity()); | |
} | |
context.SaveChanges(); | |
} | |
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>(); | |
if (!userManager.Users.Any()) | |
{ | |
foreach (var testUser in Users.Get()) | |
{ | |
var identityUser = new IdentityUser(testUser.Username) | |
{ | |
Id = testUser.SubjectId | |
}; | |
userManager.CreateAsync(identityUser, "Password123!").Wait(); | |
userManager.AddClaimsAsync(identityUser, testUser.Claims.ToList()).Wait(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment