public static async Task SeedUsers(IServiceProvider serviceProvider) { UserManager <ApplicationUser> userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); foreach (SignUpViewModel model in DefaultUsersAndRoles.GetDevelopers()) { ApplicationUser found = await userManager.FindByNameAsync(model.UserName); if (found != null) { continue; } ApplicationUser user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, }; IdentityResult result = await userManager.CreateAsync(user, model.Password); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } result = await userManager.AddToRoleAsync(user, RoleInfo.Developer); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } } }
public static async Task SeedRoles(IServiceProvider serviceProvider) { RoleManager <IdentityRole> roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); foreach (IdentityRole role in DefaultUsersAndRoles.GetRoles()) { IdentityRole found = await roleManager.FindByNameAsync(role.Name); if (found != null) { continue; } IdentityResult result = await roleManager.CreateAsync(role); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } } }