Active Directory does not find all users in C #

I have code that queries Active Directory to check for user existence. I am trying to check a long list of about 1300 identifiers. I tried several ways to check if there is a user account (LINQ to AD, DirectorySearcher (with and without a parent directory), and also DirectoryEntry, which refers to the WinNT: // path. Every time he comes back and says that several users do not exist. If I hardcode their user IDs in code and execute them individually, it checks for existence. If I try to do this in a foreach loop, I get some false negatives.

Here is the code I'm using right now.

static string[] userIDs = new string[] "user1","user2","user3","user4","user5","user6","user7","user8"...,"user1300"};

List<string> nonExistingUsers = new List<string>();
List<string> ExistingUsers = new List<string>();
foreach (string s in userIDs)
{
 DirectorySearcher search = new DirectorySearcher();
 search.Filter = String.Format("(SAMAccountName={0})", s);
 search.PropertiesToLoad.Add("cn");
 DirectorySearcher ds = new DirectorySearcher(de, "(&(objectClass=user)(cn=" + s + "))", new string[] { "Name" }, SearchScope.Subtree);
 SearchResultCollection resultCollection = ds.FindAll();
 SearchResult result = search.FindOne();
 if (result != null)
  ExistingUsers.Add(s);
 else
  nonExistingUsers.Add(s);
}

Any suggestions or reasons why I get false negatives?

+3
1

:

  • , "anr =" ( ) LDAP- - , , . " " (CN = user1)

  • -, objectCategory objectClass - objectCategory , ,

  • -: .FindAll(), .FindOne() ? -, ....

  • WinNT:// , - , , , LDAP

:

static string[] userIDs = new string[] "user1","user2","user3","user4","user5","user6","user7","user8"...,"user1300"};

DirectoryEntry searchRoot = new DirectoryEntry("LDAP://cn=Users,dc=YourComp,dc=com");

List<string> nonExistingUsers = new List<string>();
List<string> ExistingUsers = new List<string>();

foreach (string s in userIDs)
{
   DirectorySearcher search = new DirectorySearcher(searchRoot);

   search.SearchScope = SearchScope.Subtree;
   search.Filter = string.Format("(&(objectCategory=person)(anr={0}))", s);

   SearchResultCollection resultCollection = ds.FindAll();

   if(resultCollection != null && resultCollection.Count > 0)
      ExistingUsers.Add(s);
   else
      nonExistingUsers.Add(s);
}

?

, .NET 3.5 , - .:

.NET Framework 3.5

+4

Source: https://habr.com/ru/post/1724433/


All Articles