public virtual async Task SeedAsync( string adminUserPassword, IEnumerable <string> adminRolePermissions = null, Guid?tenantId = null) { const string adminUserName = "******"; const string adminRoleName = "admin"; //"admin" user var adminUser = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize(adminUserName)); if (adminUser != null) { return; } adminUser = new IdentityUser(_guidGenerator.Create(), adminUserName, "*****@*****.**", tenantId); CheckIdentityErrors(await _userManager.CreateAsync(adminUser, adminUserPassword)); //"admin" role var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName)); if (adminRole == null) { adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, tenantId); CheckIdentityErrors(await _roleManager.CreateAsync(adminRole)); if (adminRolePermissions != null) { await AddRolePermissionsAsync(adminRole, adminRolePermissions); } } CheckIdentityErrors(await _userManager.AddToRoleAsync(adminUser, adminRoleName)); }
public virtual async Task <IdentityDataSeedResult> SeedAsync( string adminEmail, string adminPassword, Guid?tenantId = null) { Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail)); Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword)); var result = new IdentityDataSeedResult(); //"admin" user const string adminUserName = "******"; var adminUser = await _userRepository.FindByNormalizedUserNameAsync( _lookupNormalizer.NormalizeName(adminUserName) ); if (adminUser != null) { return(result); } adminUser = new IdentityUser( _guidGenerator.Create(), adminUserName, adminEmail, tenantId ) { Name = adminUserName }; (await _userManager.CreateAsync(adminUser, adminPassword)).CheckErrors(); result.CreatedAdminUser = true; //"admin" role const string adminRoleName = "admin"; var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName(adminRoleName)); if (adminRole == null) { adminRole = new IdentityRole( _guidGenerator.Create(), adminRoleName, tenantId ) { IsStatic = true, IsPublic = true }; (await _roleManager.CreateAsync(adminRole)).CheckErrors(); result.CreatedAdminRole = true; } (await _userManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors(); return(result); }
public virtual async Task <IdentityDataSeedResult> SeedAsync( string adminUserPassword, Guid?tenantId = null) { var result = new IdentityDataSeedResult(); const string adminUserName = "******"; const string adminRoleName = "admin"; //"admin" user var adminUser = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize(adminUserName)); if (adminUser != null) { return(result); } adminUser = new IdentityUser(_guidGenerator.Create(), adminUserName, "*****@*****.**", tenantId); adminUser.Name = adminUserName; CheckIdentityErrors(await _userManager.CreateAsync(adminUser, adminUserPassword)); result.CreatedAdminUser = true; //"admin" role var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName)); if (adminRole == null) { adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, tenantId); adminRole.IsStatic = true; adminRole.IsPublic = true; CheckIdentityErrors(await _roleManager.CreateAsync(adminRole)); result.CreatedAdminRole = true; } CheckIdentityErrors(await _userManager.AddToRoleAsync(adminUser, adminRoleName)); return(result); }