public static void Main(string[] args) { var host = BuildWebHost(args); SeedDatabaseWithRoles(host, ROLES.Roles()); SeedDatabaseWithAdmin(host); host.Run(); }
private static void SeedDatabaseWithAdmin(IWebHost host) { using (var scope = host.Services.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <StoreSystemDbContext>(); var userManager = scope.ServiceProvider.GetRequiredService <UserManager <StoreUser> >(); var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >(); SeedDatabaseWithRoles(host, ROLES.Roles()); if (dbContext.Users.All(u => u.Email != StoreConstants.ADMIN_EMAIL)) { var adminUser = new StoreUser { UserName = StoreConstants.ADMIN_EMAIL, Email = StoreConstants.ADMIN_EMAIL }; var result = userManager.CreateAsync(adminUser, StoreConstants.ADMIN_PASSWORD).Result; if (result.Succeeded) { result = userManager.AddToRoleAsync(adminUser, ROLES.Admin).Result; } if (result.Succeeded) { //logger.LogInformation("User created a new account with password."); //var code = await userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); //await emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); //await signInManager.SignInAsync(user, isPersistent: false); //logger.LogInformation("User created a new account with password."); //return RedirectToLocal(returnUrl); } //AddErrors(result); } if (dbContext.Users.All(u => u.Email != StoreConstants.OFFICE_STAFF_EMAIL)) { var adminUser = new StoreUser { UserName = StoreConstants.OFFICE_STAFF_EMAIL, Email = StoreConstants.OFFICE_STAFF_EMAIL }; var result = userManager.CreateAsync(adminUser, StoreConstants.OFFICE_STAFF_PASSWORD).Result; if (result.Succeeded) { result = userManager.AddToRoleAsync(adminUser, ROLES.OfficeStaff).Result; } if (result.Succeeded) { //logger.LogInformation("User created a new account with password."); //var code = await userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); //await emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); //await signInManager.SignInAsync(user, isPersistent: false); //logger.LogInformation("User created a new account with password."); //return RedirectToLocal(returnUrl); } //AddErrors(result); } } }