示例#1
0
        public async Task <IActionResult> AddUserToRoles([FromBody] UserInRolesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var resp = await _userService.AddUserToRolesTaskAsync(model.UserName, model.RoleNames);

            if (!resp.Succeeded)
            {
                return(new BadRequestObjectResult(resp.Errors));
            }
            return(new OkObjectResult("New Role Created."));
        }
        [Authorize(Policy = "EditRolePolicy")] //because Handle by custom policy controls
        public async Task <IActionResult> ManageUsersRoles(string userId)
        {
            var user = await userManager.FindByIdAsync(userId);

            ViewBag.userId = user.Id;
            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with Id ={userId} cannot be found";
                return(View("../Error/NotFound"));
            }
            List <UserInRolesViewModel> listUserInRlesViewModel = new List <UserInRolesViewModel>();

            foreach (var role in roleManager.Roles)
            {
                UserInRolesViewModel rolesViewModel = new UserInRolesViewModel()
                {
                    RoleId = role.Id, RoleName = role.Name
                };
                rolesViewModel.IsSelected = await userManager.IsInRoleAsync(user, role.Name) == true ? true : false;

                listUserInRlesViewModel.Add(rolesViewModel);
            }
            return(View("../Account/ManageUserRoles", listUserInRlesViewModel));
        }