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?