public async Task <IActionResult> UpdateUserRoles(int id, [FromBody] int[] roleIds) { var applicationUser = await _applicationUserService.GetApplicationUserAsync(id); if (applicationUser != null) { foreach (var role in applicationUser.UserRoles.ToList()) { if (!roleIds.Contains(role.ApplicationRoleId)) { applicationUser.UserRoles.Remove(role); } } foreach (var roleId in roleIds) { if (!applicationUser.UserRoles.Any(x => x.ApplicationRoleId == roleId)) { applicationUser.UserRoles.Add(new UserRole { ApplicationRoleId = roleId }); } } await _applicationUserService.UpdateApplicationUserAsync(applicationUser); return(NoContent()); } return(NotFound()); }