private void CreateInitialRoleIfNotExists(List <Role> roles)
 {
     foreach (var role in roles)
     {
         if (!AsyncHelper.RunSync(() => _roleManager.RoleExistsAsync(role.Name)))
         {
             try
             {
                 AsyncHelper.RunSync(() => _roleManager.CreateAsync(role));
             }
             catch (Exception e)
             {
                 // ignored
             }
         }
     }
 }
        public async Task <IActionResult> Create(RoleDto role)
        {
            if (ModelState.IsValid)
            {
                var result = await _roleManager.CreateAsync(new AppRole { Name = role.Name, Description = role.Description });

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(item.Code, item.Description);
                }
            }

            return(View(role));
        }
        private async Task CreateDefaultRoles()
        {
            var list = new List <string>
            {
                "Admin",
                "Operator",
                "Teacher",
                "Editor"
            };

            foreach (var role in list)
            {
                if (_roleManager.Roles.All(x => x.Name != role))
                {
                    await _roleManager.CreateAsync(new Role
                    {
                        Name = role
                    });
                }
            }
        }
Esempio n. 4
0
        public async Task <(User, IdentityResult)> CreateAdminUserAsync()
        {
            var adminUser = await _userManager.FindByNameAsync(AdminUserSeedInfo.Username);

            if (adminUser != null)
            {
                _logger.LogInformation($"{AdminUserSeedInfo.Username} already existed.");
                return(adminUser,
                       IdentityResult.Success
                       );
            }

            var adminRole = await _roleManager.FindByNameAsync(AdminUserSeedInfo.RoleName);

            if (adminRole != null)
            {
                _logger.LogInformation($"{AdminUserSeedInfo.RoleName} role already existed.");
            }
            else
            {
                adminRole = new Role(AdminUserSeedInfo.RoleName);
                var roleResult = await _roleManager.CreateAsync(adminRole);

                if (roleResult == IdentityResult.Failed())
                {
                    _logger.LogError($"'{adminRole.Name}' role creating has failed. {roleResult.DumpErrors()}");
                    return(null,
                           IdentityResult.Failed()
                           );
                }
            }

            adminUser = new User {
                UserName       = AdminUserSeedInfo.Username,
                Email          = AdminUserSeedInfo.Email,
                EmailConfirmed = true,
                RegisterDate   = _dateSvc.UtcNow(),
                Title          = AdminUserSeedInfo.Title,
                LockoutEnabled = true,
                Status         = UserStatus.Enabled
            };

            var result = await _userManager.CreateAsync(adminUser, AdminUserSeedInfo.Password);

            if (result == IdentityResult.Failed())
            {
                _logger.LogError($"'{AdminUserSeedInfo.Username}; user creation has failed. {result.DumpErrors()}");
                return(null,
                       IdentityResult.Failed()
                       );
            }

            var setLockoutResult = await _userManager.SetLockoutEnabledAsync(adminUser, enabled : false);

            if (setLockoutResult == IdentityResult.Failed())
            {
                _logger.LogError($"'{AdminUserSeedInfo.Username}' setLockout on user has failed. {setLockoutResult.DumpErrors()}");
                return(null,
                       IdentityResult.Failed()
                       );
            }

            var addRoleResult = await _userManager.AddToRoleAsync(adminUser, AdminUserSeedInfo.RoleName);

            if (addRoleResult == IdentityResult.Failed())
            {
                _logger.LogError($"Adding user:'******' to role:'{AdminUserSeedInfo.RoleName}' has failed. {addRoleResult.DumpErrors()}");
                return(null,
                       IdentityResult.Failed()
                       );
            }

            return(
                adminUser,
                IdentityResult.Success
                );
        }