public IEnumerable <UserAccount> GetGroupMembers(string groupName) { try { using (var principalContext = new PrincipalContext(ContextType.Domain)) using (var groupResult = GroupPrincipal.FindByIdentity(principalContext, groupName)) { if (groupResult == null) { throw new NoMatchingPrincipalException(); } var groupResultMembers = groupResult.GetMembers(); var groupMembers = new List <UserAccount>(); var userAccountTasks = new UserAccountTasks(); foreach (var user in groupResultMembers) { groupMembers.Add(userAccountTasks.GetUserAccountDetails(user.SamAccountName)); } return(groupMembers); } } catch (NoMatchingPrincipalException e) { throw new CustomException($"Group {groupName} was not found. Error message: {e.Message}"); } }
public List <UserAccount> GetAllLockedOutAccounts() { var accountTools = new UserAccountTasks(); var lockedUsers = new List <UserAccount>(); using (var context = new PrincipalContext(ContextType.Domain)) using (var userPrincipal = new UserPrincipal(context)) using (var search = new PrincipalSearcher(userPrincipal)) { foreach (var result in search.FindAll()) { var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, result.SamAccountName); if (user != null && !user.IsAccountLockedOut()) { continue; } lockedUsers.Add(accountTools.GetUserAccountDetails(user.UserPrincipalName)); } lockedUsers.Sort(); return(lockedUsers); } }