示例#1
0
        public async Task <IHttpActionResult> AddUserToRole([FromBody] AddUserToRoleBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var role = await _roleManager.FindByNameAsync(model.RoleName);

            if (role == null)
            {
                return(NotFound());
            }
            var user = await _userManager.FindByNameAsync(model.Username);

            if (user == null)
            {
                return(NotFound());
            }
            var result = await _userManager.AddToRoleAsync(user.Id, role.Name);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            return(Ok());
        }
        public async Task <IActionResult> AddToRole(AddUserToRoleBindingModel model)
        {
            var roleExists = await this.roleManager.RoleExistsAsync(model.Role);

            var user = await this.userManager.FindByIdAsync(model.UserId);

            var userExists = user != null;

            if (!roleExists || !userExists)
            {
                this.ModelState.AddModelError(string.Empty, InvalidIdentityDetails);
            }

            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction(nameof(this.Index)));
            }

            await this.userManager.AddToRoleAsync(user, model.Role);

            //TempData.AddSuccessMessage($"User {user.UserName} successfully added to the {model.Role} role.");
            return(this.RedirectToAction(nameof(this.Index)));
        }
示例#3
0
        public async Task <IActionResult> AddToRole(AddUserToRoleBindingModel model)
        {
            var result = await this.usersService.AddUserToRoleAsync(model.UserId, model.Role);

            return(this.Json(new { success = result }));
        }