Exemplo n.º 1
0
        public async Task <IdentityResult> UpdateRolesAsync(string userName, string[] roles)
        {
            var user = await _user.FindAsync(u => u.UserName == userName);

            var assignedRoles = user.SsuserRole.Select(r => r.Role.Name);
            var rolesToAdd    = roles.Except(assignedRoles);
            var rolesToRemove = assignedRoles.Except(roles);

            var result = new IdentityResult();

            if (rolesToAdd != null && rolesToAdd.Any())
            {
                result = await _user.AddRolesToUserAsync(user, rolesToAdd);
            }

            if (!result.Succeeded)
            {
                return(result);
            }

            if (rolesToRemove != null && rolesToRemove.Any())
            {
                result = await _user.RemoveRolesFromUserAsync(user, rolesToRemove);
            }

            return(result);
        }