How do you do a query of an LDAP store by sAMAccountName and Domain? What is the "domain" property named in Active Directory or LDAP terms?
This is what I have for the filter so far. I'd like to be able to add in the domain:
(&(objectCategory=Person)(sAMAccountName=BTYNDALL))
The best way of searching for users is
(sAMAccountType=805306368)
.Or for disabled users:
(&(sAMAccountType=805306368)(userAccountControl:1.2.840.113556.1.4.803:=2))
Or for active users:
(&(sAMAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
I find LDAP as not being so light at it was supposed to be.
Also resource for common LDAP queries - trying to find them yourself and you will precious time and definitely make mistakes.
Regarding domains: it not possible in a single query because the domain is part of the user
distinguisedName
(DN
) which, on Microsoft AD, is not searchable by partial matching."Domain" is not a property of an LDAP object. It is more like the name of the database the object is stored in.
So you have to connect to the right database (in LDAP terms: "bind to the domain/directory server") in order to perform a search in that database.
Once you bound successfully, your query in it's current shape is all you need.
BTW: Choosing
"ObjectCategory=Person"
over"ObjectClass=user"
was a good decision. In AD, the former is an "indexed property" with excellent performance, the latter is not indexed and a tad slower.You can use following queries
Users whose Logon Name(Pre-Windows 2000) is equal to John
All Users
Enabled Users
Disabled Users
LockedOut Users
I have written a C# class incorporating
However, it is not Site-aware.
You have to perform your search in the domain:
http://msdn.microsoft.com/en-us/library/ms677934(VS.85).aspx So, basically your should bind to a domain in order to search inside this domain.
First, modify your search filter to only look for users and not contacts:
You can enumerate all of the domains of a forest by connecting to the configuration partition and enumerating all the entries in the partitions container. Sorry I don't have any C# code right now but here is some vbscript code I've used in the past:
From that you can retrieve the name and dnsRoot of each partition: