I have the following code that i use to search for users in the domain. When I enter a complte user name it finds it but when i use wildcards and gets even computer names that contain the string too.
How to i filter for only only users? There has to be some flag that indicates a AD objct is a user, computer, etc. I thought that what the following did "objectClass=user".
How to i filter for only only users? There has to be some flag that indicates a AD objct is a user, computer, etc. I thought that what the following did "objectClass=user".
Code:
public SearchResultCollection GetDirectoryUsers(string user)
{
try
{
DirectorySearcher searchADS = new DirectorySearcher("LDAP://mydomain.com/CN=Users,DC=mydomain,DC=com");
searchADS.Filter = "(&(objectClass=user)(SAMAccountName=" + user + "))";
searchADS.SearchScope = SearchScope.Subtree;
searchADS.PropertiesToLoad.Add("cn");
SearchResultCollection resultsAll = searchADS.FindAll();
return resultsAll;
}
catch (Exception)
{
return null;
}
}