Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 469
directedition Avatar asked Jan 13 '23 14:01

directedition


2 Answers

You need to close the opening parenthesis :

(&(objectClass=user)(|(&(SAMAccountName=jdoe*))))
like image 67
X3074861X Avatar answered Feb 25 '23 04:02

X3074861X


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");
like image 33
Scampbell Avatar answered Feb 25 '23 03:02

Scampbell