Esempio n. 1
0
        public async Task <IActionResult> AddRoleToUser(string userId)
        {
            IdentityUser user = new IdentityUser();

            if (!String.IsNullOrEmpty(userId))
            {
                user = await _userManager.FindByIdAsync(userId);
            }

            if (user == null)
            {
                return(RedirectToAction("IndexRoles", _roleManager.Roles));
            }

            // Reeds toegekende rollen
            var assignRolesToUserViewModel = new RolesForUserVM()
            {
                AssignedRoles   = await _userManager.GetRolesAsync(user),
                UnAssignedRoles = new List <string>(),
                User            = user,
                UserId          = userId
            };

            // Nog niet toegekende rollen
            foreach (var identityRole in _roleManager.Roles)
            {
                if (!await _userManager.IsInRoleAsync(user, identityRole.Name))
                {
                    assignRolesToUserViewModel.UnAssignedRoles.Add(identityRole.Name);
                }
            }
            return(View(assignRolesToUserViewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddRoleToUser(RolesForUserVM rolesForUserViewModel)
        {
            var user = await _userManager.FindByIdAsync(rolesForUserViewModel.UserId);

            var role = await _roleManager.FindByNameAsync(rolesForUserViewModel.RoleName);

            var result = await _userManager.AddToRoleAsync(user, role.Name);

            if (result.Succeeded)
            {
                return(RedirectToAction("IndexUsers", _userManager.Users));
            }
            return(View());
        }