public Task <GetUserByUserNameFromAdDto> Handle(GetUserByUserNameFromAdQuery request, CancellationToken cancellationToken) { GetUserByUserNameFromAdDto getUserByUserNameFromAdDto = null; if (!string.IsNullOrWhiteSpace(request.UserName)) { using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain)) using (DirectorySearcher search = new DirectorySearcher(entry)) { search.PageSize = 5000; search.Filter = $"(&(objectClass=user)(|(CN=something)(sAMAccountName={request.UserName})))"; SearchResult result = search.FindOne(); if (result != null) { UserAccount userAccount = ActiveDirectoryHelper.GetUserFromResult(result); if (userAccount != null) { if (!string.IsNullOrWhiteSpace(userAccount.Department) && !string.IsNullOrWhiteSpace(userAccount.SamAccountName)) { string manager = string.Empty; if (!string.IsNullOrEmpty(userAccount.Manager)) { DirectoryEntry managerEntry = new DirectoryEntry($"LDAP://{Domain}/{userAccount.Manager}"); manager = managerEntry.Properties["samAccountName"][0].ToString(); } List <string> directReports = new List <string>(); if (userAccount.DirectReports.Any()) { foreach (string directReport in directReports.Where(d => !string.IsNullOrEmpty(d))) { DirectoryEntry managerEntry = new DirectoryEntry($"LDAP://{Domain}/{directReport}"); directReports.Add(managerEntry.Properties["samAccountName"][0].ToString()); } } getUserByUserNameFromAdDto = new GetUserByUserNameFromAdDto { UserName = userAccount.SamAccountName, DepartmentName = userAccount.Department, DirectReports = string.Join(";", directReports), FirstName = userAccount.GivenName, LastName = userAccount.SurName, ManagerName = manager, Email = userAccount.Email, JpegPhoto = userAccount.JpegPhoto }; return(Task.FromResult(getUserByUserNameFromAdDto)); } } } } } return(Task.FromResult(getUserByUserNameFromAdDto)); }
public async Task <int> Handle(SyncUsersFromActiveDirectoryCommand request, CancellationToken cancellationToken) { using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain)) using (DirectorySearcher search = new DirectorySearcher(entry)) { search.PageSize = 5000; search.Filter = "(&(objectClass=user)(objectCategory=person))"; SearchResult result; SearchResultCollection resultCol = search.FindAll(); List <User> users = new List <User>(); if (resultCol != null) { for (int i = 0; i < resultCol.Count; i++) { result = resultCol[i]; AdUserAccount userAccount = ActiveDirectoryHelper.GetUserFromResult(result); if (userAccount != null) { if (!string.IsNullOrWhiteSpace(userAccount.Department) && !string.IsNullOrWhiteSpace(userAccount.SamAccountName)) { string manager = string.Empty; if (!string.IsNullOrEmpty(userAccount.Manager)) { DirectoryEntry managerEntry = new DirectoryEntry($"LDAP://{Domain}/{userAccount.Manager}"); manager = managerEntry.Properties["samAccountName"][0].ToString(); } List <string> directReports = new List <string>(); if (userAccount.DirectReports.Any()) { foreach (string directReport in directReports.Where(d => !string.IsNullOrEmpty(d))) { DirectoryEntry managerEntry = new DirectoryEntry($"LDAP://{Domain}/{directReport}"); directReports.Add(managerEntry.Properties["samAccountName"][0].ToString()); } } List <string> groups = new List <string>(); var userPrinciple = ActiveDirectoryHelper.GetUser(System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, userAccount.SamAccountName); groups = ActiveDirectoryHelper.GetGroupsForUser(userPrinciple).ToList(); users.Add(new User { UserName = userAccount.SamAccountName, Active = userAccount.Active, CreatedBy = request.CreatedBy, CreatedOn = request.CreatedOn, DepartmentName = userAccount.Department, DirectReports = string.Join(";", directReports), Groups = string.Join(";", groups), FirstName = userAccount.GivenName, LastName = userAccount.SurName, Email = userAccount.UserPrincipalName, JpegPhoto = userAccount.JpegPhoto, ManagerName = manager }); } } } } _context.Users.AddOrUpdate(user => new { user.UserName, user.DepartmentName }, users.ToArray()); return(await _context.SaveChangesAsync(cancellationToken)); } }