コード例 #1
0
        public async Task <ActionResult> RemoveRole(RoleChangeClass roleChange)
        {
            User user = await userManager.FindByNameAsync(roleChange.UserName);

            IdentityRole role = await roleManager.FindByNameAsync(roleChange.Role);

            if (role == null)
            {
                return(NotFound("Role not found"));
            }

            if (!(await userManager.IsInRoleAsync(user, roleChange.Role)))
            {
                return(BadRequest($"{roleChange.UserName} does not have role {roleChange.Role}"));
            }

            await userManager.RemoveFromRoleAsync(user, roleChange.Role);

            return(Ok($"Successfully removed {roleChange.UserName} from role {roleChange.Role}"));
        }
コード例 #2
0
        public async Task <ActionResult> AddRole(RoleChangeClass roleChange)
        {
            User user = await userManager.FindByNameAsync(roleChange.UserName);

            IdentityRole role = await roleManager.FindByNameAsync(roleChange.Role);

            if (role == null)
            {
                return(NotFound("Role not found"));
            }

            if (await userManager.IsInRoleAsync(user, roleChange.Role))
            {
                return(BadRequest($"{roleChange.UserName} already has role {roleChange.Role}"));
            }

            await userManager.AddToRoleAsync(user, roleChange.Role);

            return(Ok($"Successfully added {roleChange.UserName} to role {roleChange.Role}"));
        }