DirectorySearcher User search filter is invalid

active-directory, ldap

Solution

If you are using .Net 3.5 or later, you can use a UserPrincipal object to get user information, like this.

PrincipalContext pcontext = new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal user = UserPrincipal.FindByIdentity(pcontext,IdentityType.SamAccountName, "UserName");

Problem

The (&(objectClass=user)(|(&(SAMAccountName=jdoe*))) search filter is invalid. I'm trying to locate John Doe's user account by his username, jdoe. I've used a number of variants of this search string, and they all return this error. What am I doing wrong? I'm building it out like this: ``` var deSearch = new DirectorySearcher(de); deSearch.Filter = string.Format("(&(objectClass=user)(|(&(SAMAccountName={0}*)))", uname); SearchResult result = deSearch.FindOne(); ```

Original source

Related problems