Complete Guide to Active Directory LDAP Query by sAMAccountName and Domain

Nov 22, 2025 · Programming · 10 views · 7.8

Keywords: Active Directory | LDAP | sAMAccountName | Domain Query | Configuration Partition

Abstract: This article provides a comprehensive exploration of LDAP queries in Active Directory using sAMAccountName and domain parameters. It explains the concepts of sAMAccountName and domain in AD, presents optimized search filters including exclusion of contact objects, and details domain enumeration through configuration partitions with code examples. Additional common user query scenarios such as enabled/disabled users and locked accounts are also discussed.

LDAP Query Fundamentals

In Active Directory environments, LDAP (Lightweight Directory Access Protocol) serves as the primary method for accessing directory services. The sAMAccountName attribute represents the pre-Windows 2000 logon name used to identify user accounts. Domains in Active Directory define security boundaries, with user accounts belonging to specific domains.

Optimized Search Filters

The original query filter (&(objectCategory=Person)(sAMAccountName=BTYNDALL)) can be enhanced by adding objectClass=user to ensure only user objects are searched, excluding contacts:

(&(objectCategory=person)(objectClass=user)(sAMAccountName=BTYNDALL))

Domain Enumeration Methods

In Active Directory, domain information is stored within the configuration partition. By connecting to the configuration partition and querying the partitions container, all domains in the forest can be enumerated. Below is a VBScript code example:

Set objRootDSE = GetObject("LDAP://RootDSE")
AdComm.Properties("Sort on") = "name"
AdComm.CommandText = "<LDAP://cn=Partitions," & _
    objRootDSE.Get("ConfigurationNamingContext") & ">;" & _
        "(&(objectcategory=crossRef)(systemFlags=3));" & _
            "name,nCName,dnsRoot;onelevel"
set AdRs = AdComm.Execute

Extracting Domain Information

Extract domain names and DNS root information from query results:

AdRs.MoveFirst
With AdRs
  While Not .EOF
    dnsRoot = .Fields("dnsRoot")

    Set objOption = Document.createElement("OPTION")
    objOption.Text = dnsRoot(0)
    objOption.Value = "LDAP://" & dnsRoot(0) & "/" & .Fields("nCName").Value
    Domain.Add(objOption)
    .MoveNext 
  Wend 
End With

Other Common Query Scenarios

Beyond basic user queries, LDAP supports various filtering conditions:

Users with logon name John:

(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(sAMAccountName=John))

All users:

(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370))

Enabled users:

(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(!userAccountControl:1.2.840.113556.1.4.803:=2))

Disabled users:

(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(userAccountControl:1.2.840.113556.1.4.803:=2))

Locked out users:

(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(lockouttime>=1))

Alternative Query Methods

Another approach for searching users utilizes sAMAccountType=805306368, specifically designed for user objects:

(sAMAccountType=805306368)

Alternative query for disabled users:

(&(sAMAccountType=805306368)(userAccountControl:1.2.840.113556.1.4.803:=2))

Alternative query for active users:

(&(sAMAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))

Practical Application Considerations

In practical applications, usernames might be provided in "domain\username" format. Since the domain is part of the user's distinguished name (DN), partial matching searches are not supported in Microsoft AD. Therefore, domain name stripping must be handled at the application layer, or domain enumeration methods described in this article should be used to construct complete queries.

Performance Optimization Recommendations

LDAP query performance is influenced by multiple factors including filter complexity, returned attribute count, and network latency. Recommendations include:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.