public async Task <ActionResult> EditActiveDirectoryUser(UserDetail userDetail) { if (ModelState.IsValid) { ViewBag.SystemAdmin = false; var identity = (System.Security.Claims.ClaimsIdentity)User.Identity; var claims = identity.Claims; string SystemAdmin = claims.FirstOrDefault(x => x.Type == "SystemAdmin")?.Value; if (SystemAdmin == "True") { ViewBag.SystemAdmin = true; } var token = GetToken(); userDetail.Roles = new List <RoleDetail>(); if (userDetail.RoleIds == null) { var message = "Must select at least one role for user."; throw new InvalidOperationException(message); } if (userDetail.RoleIds.Count > 0) { var userRoles = new List <RoleDetail>(); foreach (var id in userDetail.RoleIds) { var roles = await IzendaUtilities.GetRoles(token); var roleId = roles.FirstOrDefault(i => i.Id == Guid.Parse(id)).Id.ToString(); var role = roles.Where(i => i.Id == Guid.Parse(roleId)).Select(v => new RoleDetail { Id = v.Id, Name = v.Name }).FirstOrDefault(); if (roleId != null) { try { userDetail.Roles.Add(role); userRoles.Add(role); } catch (Exception e) { Console.WriteLine(e); throw; } } } var allRoles = await IzendaUtilities.GetRoles(token); var rolesToRemove = allRoles.Except(userRoles); foreach (var role in rolesToRemove) { try { userDetail.Roles.Remove(role); } catch (Exception e) { Console.WriteLine(e); throw; } } } try { var success = false; var izendaUsers = new List <UserDetail>(); izendaUsers = await IzendaUtilities.IzendaGetAllUsers(token); var currentUser = izendaUsers.FirstOrDefault(x => x.EmailAddress == userDetail.EmailAddress); if (currentUser == null) { success = await IzendaUtilities.SaveIzendaUser(token, userDetail); if (success) { var combinedList = CacheSystem.GetCacheItem("combinedList") as List <UserDetail>; var duplicateUserDetail = combinedList.FirstOrDefault(e => e.EmailAddress == userDetail.EmailAddress); combinedList.Remove(duplicateUserDetail); combinedList.Add(userDetail); return(PartialView("ActiveDirectoryList", combinedList)); } } if (currentUser != null && currentUser.Active != userDetail.Active) { if (currentUser.Active && !userDetail.Active) { currentUser = await IzendaUtilities.DeactivateIzendaUser(token, userDetail); } if (!currentUser.Active && userDetail.Active) { currentUser = await IzendaUtilities.ActivateIzendaUser(token, userDetail); } if (currentUser.Active == userDetail.Active) { success = await IzendaUtilities.SaveIzendaUser(token, currentUser); } } if (currentUser.Active == userDetail.Active) { success = await IzendaUtilities.SaveIzendaUser(token, currentUser); } if (success) { var combinedList = CacheSystem.GetCacheItem("combinedList") as List <UserDetail>; var duplicateUserDetail = combinedList.FirstOrDefault(e => e.EmailAddress == userDetail.EmailAddress); combinedList.Remove(duplicateUserDetail); combinedList.Add(userDetail); return(PartialView("ActiveDirectoryList", combinedList)); } } catch (Exception e) { Console.WriteLine(e); throw; } return(View(userDetail)); } else { ViewBag.SystemAdmin = false; var identity = (System.Security.Claims.ClaimsIdentity)User.Identity; var claims = identity.Claims; string SystemAdmin = claims.FirstOrDefault(x => x.Type == "SystemAdmin")?.Value; if (SystemAdmin == "True") { ViewBag.SystemAdmin = true; } var token = GetToken(); var roles = await IzendaUtilities.GetRoles(token); var employeeList = CacheSystem.GetCacheItem("combinedList") as List <UserDetail>; var employee = (from e in employeeList where e.EmailAddress == userDetail.EmailAddress select e).FirstOrDefault(); var userRoles = roles.Where(t => t.Users.Contains(employee)).ToList(); var userRolesIds = new string[userRoles.Count]; var length = userRoles.Count; for (var i = 0; i < length; i++) { userRolesIds[i] = userRoles[i].Id.ToString(); } var rolesList = new MultiSelectList(roles.ToList().OrderBy(i => i.Name), "Id", "Name", userRolesIds); employee.RoleOptions = rolesList; return(View(employee)); } }