API Reference
LdapQueryHelper
Provides centralized LDAP query functionality for Active Directory operations. Handles domain/forest iteration, property extraction, error handling, and result mapping.
Inheritance
- Object
- LdapQueryHelper
Remarks
See usage examples in the unit tests for typical scenarios.
Methods
public static T CreateWithParameterMapping<T>(IDictionary<String, Object> parameters) #TType Parameters
- T
Parameters
- parameters IDictionary<String, Object>
CreateWithParameterMapping``1(System.Collections.Generic.IDictionary{System.String,System.Object} parameters) #Creates an instance of a class and maps parameters to constructor
Type Parameters
- T
- Type to create
Parameters
- parameters System.Collections.Generic.IDictionary{System.String,System.Object}
- Parameter dictionary
Returns
Created instance
public static Nullable<Boolean> GetBoolProperty(SearchResult result, String propertyName) #Nullable<Boolean>Safely extracts boolean property from SearchResult with null handling
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static Nullable<DateTime> GetDateTimeProperty(SearchResult result, String propertyName) #Nullable<DateTime>Safely extracts DateTime property from SearchResult with null handling
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static Nullable<DateTime> GetFileTimeProperty(SearchResult result, String propertyName) #Nullable<DateTime>Safely extracts FileTime DateTime property from SearchResult
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static Nullable<Int32> GetIntProperty(SearchResult result, String propertyName) #Nullable<Int32>Safely extracts integer property from SearchResult with null handling
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static Nullable<Int64> GetLongProperty(SearchResult result, String propertyName) #Nullable<Int64>Safely extracts a 64-bit integer property from SearchResult with null handling.
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static String[] GetMultiStringProperty(SearchResult result, String propertyName) #String[]Safely extracts multi-value string property from SearchResult
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static String GetSidStringProperty(SearchResult result, String propertyName = "objectSid") #StringSafely extracts SID property (as string) from SearchResult, handling byte[] and SID objects.
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String = "objectSid"
public static String GetStringProperty(SearchResult result, String propertyName) #StringSafely extracts string property from SearchResult with null handling
Parameters
- result System.DirectoryServices.SearchResult
- propertyName System.String
public static IEnumerable<String> GetTargetDomains(String domainName = null, String forestName = null) #IEnumerable<String>Gets domains to query based on parameters with automatic null handling.
Parameters
- domainName System.String = null
- Specific domain to query. If null, queries all domains in forest.
- forestName System.String = null
- Specific forest to target. If null, uses current forest.
Returns
Collection of domain names to query.
public static Forest GetTargetForest(String forestName = null) #ForestGets forest context with automatic fallback to current forest.
Parameters
- forestName System.String = null
- Specific forest name. If null, returns current forest.
Returns
Forest context for the specified or current forest.
public static Boolean HasUserAccountControlFlag(SearchResult result, Int32 flag, String propertyName = "userAccountControl") #BooleanChecks if userAccountControl flag is set
Parameters
- result System.DirectoryServices.SearchResult
- flag System.Int32
- propertyName System.String = "userAccountControl"
public static TResult InvokeWithParameterMapping<TResult>(MethodInfo method, IDictionary<String, Object> parameters, Object instance = null) #TResultType Parameters
- TResult
Parameters
- method MethodInfo
- parameters IDictionary<String, Object>
- instance Object = null
InvokeWithParameterMapping``1(System.Reflection.MethodInfo method, System.Collections.Generic.IDictionary{System.String,System.Object} parameters, System.Object instance) #Automatically maps dictionary parameters to method parameters using reflection Similar to the TestimoX MethodInfo wrapper but optimized for LDAP scenarios
Type Parameters
- TResult
- Return type of the method
Parameters
- method System.Reflection.MethodInfo
- Method to invoke
- parameters System.Collections.Generic.IDictionary{System.String,System.Object}
- Parameter dictionary
- instance System.Object
- Instance to invoke on (null for static methods)
Returns
Method result
public static IEnumerable<T> Query<T>(String ldapPath, String filter, String[] propertiesToLoad, Func<SearchResult, T> resultMapper, SearchScope searchScope = Subtree, Int32 pageSize = 1000, Int32 sizeLimit = 0, SecurityMasks securityMasks = None) #IEnumerable<T>Type Parameters
- T
Parameters
- ldapPath String
- filter String
- propertiesToLoad String[]
- resultMapper Func<SearchResult, T>
- searchScope SearchScope = Subtree
- pageSize Int32 = 1000
- sizeLimit Int32 = 0
- securityMasks SecurityMasks = None
public static Task<IEnumerable<T>> QueryAsync<T>(String ldapPath, String filter, String[] propertiesToLoad, Func<SearchResult, T> resultMapper, SearchScope searchScope = Subtree, Int32 pageSize = 1000, CancellationToken cancellationToken = null, Int32 sizeLimit = 0, SecurityMasks securityMasks = None) #Task<IEnumerable<T>>Type Parameters
- T
Parameters
- ldapPath String
- filter String
- propertiesToLoad String[]
- resultMapper Func<SearchResult, T>
- searchScope SearchScope = Subtree
- pageSize Int32 = 1000
- cancellationToken CancellationToken = null
- sizeLimit Int32 = 0
- securityMasks SecurityMasks = None
QueryAsync``1(System.String ldapPath, System.String filter, System.String[] propertiesToLoad, System.Func{System.DirectoryServices.SearchResult,``0} resultMapper, System.DirectoryServices.SearchScope searchScope, System.Int32 pageSize, System.Threading.CancellationToken cancellationToken, System.Int32 sizeLimit, System.DirectoryServices.SecurityMasks securityMasks) #Asynchronously executes an LDAP query against the provided path.
Type Parameters
- T
- Result type to map to.
Parameters
- ldapPath System.String
- LDAP path or domain name.
- filter System.String
- LDAP filter.
- propertiesToLoad System.String[]
- Properties to load from AD.
- resultMapper System.Func{System.DirectoryServices.SearchResult,``0}
- Function to map SearchResult to the result type.
- searchScope System.DirectoryServices.SearchScope
- Scope for the directory search.
- pageSize System.Int32
- Page size for large queries.
- cancellationToken System.Threading.CancellationToken
- Cancellation token.
- sizeLimit System.Int32
- Optional server-side size limit (0 = no limit).
- securityMasks System.DirectoryServices.SecurityMasks
- Security masks to request (e.g., Owner).
Returns
Mapped results from the query.
public static IEnumerable<T> QueryDomain<T>(String domainName, String filter, String[] propertiesToLoad, Func<SearchResult, T> resultMapper, SearchScope searchScope = Subtree, Int32 pageSize = 1000, Int32 sizeLimit = 0, SecurityMasks securityMasks = None) #IEnumerable<T>Type Parameters
- T
Parameters
- domainName String
- filter String
- propertiesToLoad String[]
- resultMapper Func<SearchResult, T>
- searchScope SearchScope = Subtree
- pageSize Int32 = 1000
- sizeLimit Int32 = 0
- securityMasks SecurityMasks = None
public static async Task<IEnumerable<T>> QueryDomainAsync<T>(String domainName, String filter, String[] propertiesToLoad, Func<SearchResult, T> resultMapper, SearchScope searchScope = Subtree, Int32 pageSize = 1000, CancellationToken cancellationToken = null, Int32 sizeLimit = 0, SecurityMasks securityMasks = None) #Task<IEnumerable<T>>Type Parameters
- T
Parameters
- domainName String
- filter String
- propertiesToLoad String[]
- resultMapper Func<SearchResult, T>
- searchScope SearchScope = Subtree
- pageSize Int32 = 1000
- cancellationToken CancellationToken = null
- sizeLimit Int32 = 0
- securityMasks SecurityMasks = None
QueryDomainAsync``1(System.String domainName, System.String filter, System.String[] propertiesToLoad, System.Func{System.DirectoryServices.SearchResult,``0} resultMapper, System.DirectoryServices.SearchScope searchScope, System.Int32 pageSize, System.Threading.CancellationToken cancellationToken, System.Int32 sizeLimit, System.DirectoryServices.SecurityMasks securityMasks) #Asynchronously executes a single-domain LDAP query with automatic result processing.
Type Parameters
- T
- Result type to map to
Parameters
- domainName System.String
- Domain to query
- filter System.String
- LDAP filter
- propertiesToLoad System.String[]
- Properties to load from AD
- resultMapper System.Func{System.DirectoryServices.SearchResult,``0}
- Function to map SearchResult to T
- searchScope System.DirectoryServices.SearchScope
- Scope for the directory search
- pageSize System.Int32
- Page size for large queries (default: 1000)
- cancellationToken System.Threading.CancellationToken
- Cancellation token.
- sizeLimit System.Int32
- Optional server-side size limit (0 = no limit).
- securityMasks System.DirectoryServices.SecurityMasks
- Security masks to request (e.g., Owner).
Returns
Mapped results
QueryDomain``1(System.String domainName, System.String filter, System.String[] propertiesToLoad, System.Func{System.DirectoryServices.SearchResult,``0} resultMapper, System.DirectoryServices.SearchScope searchScope, System.Int32 pageSize, System.Int32 sizeLimit, System.DirectoryServices.SecurityMasks securityMasks) #Executes a single-domain LDAP query with automatic result processing.
Type Parameters
- T
- Result type to map to
Parameters
- domainName System.String
- Domain to query
- filter System.String
- LDAP filter
- propertiesToLoad System.String[]
- Properties to load from AD
- resultMapper System.Func{System.DirectoryServices.SearchResult,``0}
- Function to map SearchResult to T
- searchScope System.DirectoryServices.SearchScope
- Scope for the directory search
- pageSize System.Int32
- Page size for large queries (default: 1000)
- sizeLimit System.Int32
- Optional server-side size limit (0 = no limit).
- securityMasks System.DirectoryServices.SecurityMasks
- Security masks to request (e.g., Owner).
Returns
Mapped results
public static IEnumerable<T> QueryMultiDomain<T>(String filter, String[] propertiesToLoad, Func<SearchResult, T> resultMapper, String domainName = null, String forestName = null, SearchScope searchScope = Subtree, Int32 pageSize = 1000, CancellationToken cancellationToken = null, Int32 sizeLimit = 0, SecurityMasks securityMasks = None) #IEnumerable<T>Type Parameters
- T
Parameters
- filter String
- propertiesToLoad String[]
- resultMapper Func<SearchResult, T>
- domainName String = null
- forestName String = null
- searchScope SearchScope = Subtree
- pageSize Int32 = 1000
- cancellationToken CancellationToken = null
- sizeLimit Int32 = 0
- securityMasks SecurityMasks = None
public static async Task<IEnumerable<T>> QueryMultiDomainAsync<T>(String filter, String[] propertiesToLoad, Func<SearchResult, T> resultMapper, String domainName = null, String forestName = null, SearchScope searchScope = Subtree, Int32 pageSize = 1000, CancellationToken cancellationToken = null, Int32 sizeLimit = 0, SecurityMasks securityMasks = None) #Task<IEnumerable<T>>Type Parameters
- T
Parameters
- filter String
- propertiesToLoad String[]
- resultMapper Func<SearchResult, T>
- domainName String = null
- forestName String = null
- searchScope SearchScope = Subtree
- pageSize Int32 = 1000
- cancellationToken CancellationToken = null
- sizeLimit Int32 = 0
- securityMasks SecurityMasks = None
QueryMultiDomainAsync``1(System.String filter, System.String[] propertiesToLoad, System.Func{System.DirectoryServices.SearchResult,``0} resultMapper, System.String domainName, System.String forestName, System.DirectoryServices.SearchScope searchScope, System.Int32 pageSize, System.Threading.CancellationToken cancellationToken, System.Int32 sizeLimit, System.DirectoryServices.SecurityMasks securityMasks) #Asynchronously executes multi-domain LDAP query across the forest with parallel domain iteration. Handles cancellation and aggregates any thrown exceptions.
Type Parameters
- T
- Result type to map to
Parameters
- filter System.String
- LDAP filter
- propertiesToLoad System.String[]
- Properties to load from AD
- resultMapper System.Func{System.DirectoryServices.SearchResult,``0}
- Function to map SearchResult to T
- domainName System.String
- Specific domain (null for all domains in forest)
- forestName System.String
- Specific forest (null for current forest)
- searchScope System.DirectoryServices.SearchScope
- Scope for the directory search.
- pageSize System.Int32
- Page size for large queries (default: 1000)
- cancellationToken System.Threading.CancellationToken
- Cancellation token.
- sizeLimit System.Int32
- Optional server-side size limit (0 = no limit).
- securityMasks System.DirectoryServices.SecurityMasks
- Security masks to request (e.g., Owner).
Returns
Mapped results from all target domains
QueryMultiDomain``1(System.String filter, System.String[] propertiesToLoad, System.Func{System.DirectoryServices.SearchResult,``0} resultMapper, System.String domainName, System.String forestName, System.DirectoryServices.SearchScope searchScope, System.Int32 pageSize, System.Threading.CancellationToken cancellationToken, System.Int32 sizeLimit, System.DirectoryServices.SecurityMasks securityMasks) #Executes multi-domain LDAP query across the forest with parallel domain iteration. Handles cancellation and aggregates any thrown exceptions.
Type Parameters
- T
- Result type to map to
Parameters
- filter System.String
- LDAP filter
- propertiesToLoad System.String[]
- Properties to load from AD
- resultMapper System.Func{System.DirectoryServices.SearchResult,``0}
- Function to map SearchResult to T
- domainName System.String
- Specific domain (null for all domains in forest)
- forestName System.String
- Specific forest (null for current forest)
- searchScope System.DirectoryServices.SearchScope
- Scope for the directory search.
- pageSize System.Int32
- Page size for large queries (default: 1000)
- cancellationToken System.Threading.CancellationToken
- Cancellation token.
- sizeLimit System.Int32
- Optional server-side size limit (0 = no limit).
- securityMasks System.DirectoryServices.SecurityMasks
- Security masks to request (e.g., Owner).
Returns
Mapped results from all target domains
Query``1(System.String ldapPath, System.String filter, System.String[] propertiesToLoad, System.Func{System.DirectoryServices.SearchResult,``0} resultMapper, System.DirectoryServices.SearchScope searchScope, System.Int32 pageSize, System.Int32 sizeLimit, System.DirectoryServices.SecurityMasks securityMasks) #Executes an LDAP query against the provided path.
Type Parameters
- T
- Result type to map to.
Parameters
- ldapPath System.String
- LDAP path or domain name.
- filter System.String
- LDAP filter.
- propertiesToLoad System.String[]
- Properties to load from AD.
- resultMapper System.Func{System.DirectoryServices.SearchResult,``0}
- Function to map SearchResult to the result type.
- searchScope System.DirectoryServices.SearchScope
- Scope for the directory search.
- pageSize System.Int32
- Page size for large queries.
- sizeLimit System.Int32
- Optional server-side size limit (0 = no limit).
- securityMasks System.DirectoryServices.SecurityMasks
- Security masks to request (e.g., Owner).
Returns
Mapped results from the query.