I use a DirectorySearcher
to get all users from Active Directory - but I need to get only "real" users.
Filter:
search.Filter = "(&(objectClass=user)(objectCategory=person))";
but I get all user accounts, like:
henry.miller <-wanted
ernest.hemingway <-wanted
HealthMailboxced7671 <-not wanted
Question: how does my filter need to be modified to return only real users?
My whole code:
string DomainPath = "LDAP://DC=writers,DC=local";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains("samaccountname"))
{
Console.WriteLine((String)result.Properties["samaccountname"][0]);
}
}
}