API Reference
DomainHelper
Conversion helpers for domain names and distinguished names.
Inheritance
- Object
- DomainHelper
Methods
public static String DistinguishedNameToDomainName(String distinguishedName) #StringConverts a distinguished name to a domain name.
Parameters
- distinguishedName System.String
- Distinguished name of the domain.
Returns
Domain name string.
public static String DomainNameToDistinguishedName(String domain) #StringConverts a domain name to its distinguished name.
Parameters
- domain System.String
- Domain name or distinguished name.
Returns
Distinguished name of the domain.
public static IEnumerable<String> EnumerateDomainControllers(String domainName = null, Func<DomainController, Boolean> filter = null, NetworkCredential credential = null, CancellationToken cancellationToken = null) #IEnumerable<String>Enumerates domain controller host names for a domain or the whole forest.
Parameters
- domainName System.String = null
- Optional domain name.
- filter System.Func{System.DirectoryServices.ActiveDirectory.DomainController,System.Boolean} = null
- Optional filter applied to each domain controller.
- credential System.Net.NetworkCredential = null
- Optional alternate credential for DirectoryServices lookups.
- cancellationToken System.Threading.CancellationToken = null
- Token to cancel the operation.
Returns
Collection of domain controller names.
public static IAsyncEnumerable<String> EnumerateDomainControllersAsync(String domainName = null, Func<DomainController, Boolean> filter = null, NetworkCredential credential = null, CancellationToken cancellationToken = null) #IAsyncEnumerable<String>Asynchronously enumerates domain controller host names for a domain or the whole forest.
Parameters
- domainName System.String = null
- Optional domain name.
- filter System.Func{System.DirectoryServices.ActiveDirectory.DomainController,System.Boolean} = null
- Optional filter applied to each domain controller.
- credential System.Net.NetworkCredential = null
- Optional alternate credential for DirectoryServices lookups.
- cancellationToken System.Threading.CancellationToken = null
- Token to cancel the operation.
Returns
Asynchronous collection of domain controller names.
EnumerateDomainControllersUnfiltered(System.String arg1, System.Net.NetworkCredential arg2, System.Threading.CancellationToken arg3) #Enumerates domain controller host names without consulting the LDAP engine's temporary ban list. Intended for last‑resort selection paths that still want a concrete host instead of a serverless bind.
Parameters
- arg1 System.String
- arg2 System.Net.NetworkCredential
- arg3 System.Threading.CancellationToken
public static IEnumerable<String> EnumerateDomainControllersViaDnsSrv(String domainName) #IEnumerable<String>Enumerates domain controllers via DNS SRV (_ldap._tcp.dc._msdcs) without LDAP or domain join.
Parameters
- domainName System.String
public static IEnumerable<String> EnumerateDomainControllersViaDsGetDcName(String domainName) #IEnumerable<String>Attempts to locate a domain controller via DsGetDcName (DNS-based, works when machine is not joined).
Parameters
- domainName System.String
public static IEnumerable<String> EnumerateDomainControllersViaLdap(String domainName, CancellationToken cancellationToken = null) #IEnumerable<String>Enumerates domain controllers for a domain via LDAP by scanning CN=Sites servers and selecting matching dNSHostName suffixes.
Parameters
- domainName System.String
- cancellationToken System.Threading.CancellationToken = null
public static IEnumerable<String> EnumerateForestDomainNames(String forestName = null, CancellationToken cancellationToken = null) #IEnumerable<String>Enumerates domain names for the specified forest.
Parameters
- forestName System.String = null
- Optional forest name.
- cancellationToken System.Threading.CancellationToken = null
- Token to cancel the operation.
Returns
Collection of domain names.
public static IEnumerable<String> EnumerateForestDomainNames(String forestName, Boolean includeTrustedDomains, CancellationToken cancellationToken = null) #IEnumerable<String>Enumerates domain names for the specified forest, optionally including trusted forests.
Parameters
- forestName System.String
- Optional forest name.
- includeTrustedDomains System.Boolean
- Whether to include trusted forest domains.
- cancellationToken System.Threading.CancellationToken = null
- Token to cancel the operation.
Returns
Collection of domain names.
public static IAsyncEnumerable<String> EnumerateForestDomainNamesAsync(String forestName = null, CancellationToken cancellationToken = null) #IAsyncEnumerable<String>Asynchronously enumerates domain names for the specified forest.
Parameters
- forestName System.String = null
- Optional forest name.
- cancellationToken System.Threading.CancellationToken = null
- Token to cancel the operation.
Returns
Asynchronous collection of domain names.
public static IEnumerable<String> EnumerateForestDomainNamesViaLdap(String forestName = null, CancellationToken cancellationToken = null) #IEnumerable<String>Enumerates forest domain DNS names via LDAP (CN=Partitions) to avoid SDA hangs. Uses crossRef objects with the dnsRoot attribute.
Parameters
- forestName System.String = null
- cancellationToken System.Threading.CancellationToken = null
public static Domain GetDomain(String domainName = null) #DomainRetrieves a domain by name or the current domain when no name is provided.
Parameters
- domainName System.String = null
- Optional domain name.
Returns
The Domain object.
public static DomainController GetDomainController(String nameOrDn) #DomainControllerAttempts to locate a domain controller by name or distinguished name.
Parameters
- nameOrDn System.String
- Name or distinguished name.
Returns
Domain controller instance.
public static DomainMode GetDomainFunctionalLevel(String domainName) #DomainModeGets the functional level for the specified domain.
Parameters
- domainName System.String
- Name of the domain to query.
Returns
The DomainMode for the domain or Unknown when unavailable.
public static String GetDomainNameFromNetBiosValue(String netBiosName) #StringRetrieves the DNS domain name for a NetBIOS domain name by reading CN=Partitions crossRef (dnsRoot). Returns null when it cannot be resolved.
Parameters
- netBiosName System.String
- NetBIOS domain name.
Exceptions
ArgumentNullException– Thrown when netBiosName is null.ArgumentException– Thrown when netBiosName is empty or whitespace.
public static String GetDomainSidValue(String domainName) #StringRetrieves the SID value for the specified domain.
Parameters
- domainName System.String
- Domain to query.
Returns
Domain SID string or null on failure.
public static Forest GetForest(String forestName = null) #ForestRetrieves a forest by name or the current forest when no name is provided.
Parameters
- forestName System.String = null
- Optional forest name.
Returns
The Forest object.
public static String GetLocalMachineSidValue() #StringRetrieves the local machine SID prefix (S-1-5-21-...) when available.
public static String GetNetBiosNameValue(String domainName) #StringRetrieves the NetBIOS name for the specified DNS domain name by reading CN=Partitions crossRef (nETBIOSName). Returns null if it cannot be resolved.
Parameters
- domainName System.String
- DNS domain name.
public static Boolean IsReadOnlyDc(String dcName) #BooleanDetermines whether the specified domain controller is read only.
Parameters
- dcName System.String
- Domain controller host name.
Returns
true when the DC is read only.
public static Boolean TryGetCurrentDomain(out Domain domain) #BooleanAttempts to get the current domain object.
Parameters
- domain System.DirectoryServices.ActiveDirectory.Domain@
- When the method returns, contains the domain object.
Returns
true when the machine is domain-joined.
public static Boolean TryGetCurrentDomainName(out String domainName) #BooleanAttempts to get the current domain name.
Parameters
- domainName System.String@
- When the method returns, contains the domain name.
Returns
true when the machine is domain-joined.
public static Boolean TryGetCurrentForest(out Forest forest, Int32 timeoutSeconds = 3) #BooleanAttempts to get the current forest within a bounded time budget.
Parameters
- forest System.DirectoryServices.ActiveDirectory.Forest@
- timeoutSeconds System.Int32 = 3
public static Boolean TryGetLocalSiteName(out String site) #BooleanTries to resolve the local AD site name of this computer.
Parameters
- site System.String@
public static String TryGetPdcHostName(String domainName) #StringAttempts to resolve the PDC Emulator host name for a domain. Returns null on failure.
Parameters
- domainName System.String
Inherited Methods
Properties
public static Forest CurrentForest { get; } #Gets the current forest with a short safety timeout to avoid hangs on unhealthy DCs. The first successful call caches the Forest instance for reuse. Throws on timeout; callers should prefer Int32) when possible.
public static Domain RootDomain { get; } #Gets the root domain of the current forest using lazy initialization.
public static String RootDomainName { get; } #Gets the name of the root domain using lazy initialization.