directedition
directedition

Reputation: 11705

DirectorySearcher User search filter is invalid

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();

Upvotes: 4

Views: 11822

Answers (2)

Scampbell
Scampbell

Reputation: 1575

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");

Upvotes: 3

X3074861X
X3074861X

Reputation: 3819

You need to close the opening parenthesis :

(&(objectClass=user)(|(&(SAMAccountName=jdoe*))))

Upvotes: 6

Related Questions