This topic has not yet been rated - Rate this topic

DirectoryContext Class

The DirectoryContext class identifies a specific directory and the credentials that are used to access the directory.

System.Object
  System.DirectoryServices.ActiveDirectory.DirectoryContext

Namespace:  System.DirectoryServices.ActiveDirectory
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)
[EnvironmentPermissionAttribute(SecurityAction.Assert, Unrestricted = true)]
public class DirectoryContext

The DirectoryContext type exposes the following members.

  Name Description
Public method DirectoryContext(DirectoryContextType) Initializes a new instance of the DirectoryContext class of the specified type that contains the credentials of the current user context.
Public method DirectoryContext(DirectoryContextType, String) Initializes a new instance of the DirectoryContext class of the specified type that contains the specified name and the credentials of the current user context.
Public method DirectoryContext(DirectoryContextType, String, String) Initializes a new instance of the DirectoryContext class of the specified type that contains the specified user name and password.
Public method DirectoryContext(DirectoryContextType, String, String, String) Initializes a new instance of the DirectoryContext class of the specified type that contains the specified target, user name, and password.
Top
  Name Description
Public property ContextType Gets the type of the context object.
Public property Name Gets the name of the context.
Public property UserName Gets the user name of the context.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

A directory context must be created before accessing the methods and properties of a System.DirectoryServices.ActiveDirectory directory object such as a domain or forest.

When creating a directory context, the process' current credentials or alternate credentials can be applied to that context by using the appropriate constructor. These credentials determine the permissions that are in effect when the program accesses directory objects. Because Active Directory Domain Services objects might be protected against retrieval or modification by non-administrative users, the use of alternate credentials with permission to access the target objects is sometimes necessary for proper program functionality.

The following table contains a list of the target types that are allowed and a description of the format of the target string.

Target type

DirectoryContextType member

Target name format

Domain Controller

DirectoryServer

The DNS name of the domain controller.

AD LDS Instance

DirectoryServer

The DNS name of the AD LDS server and the LDAP port number, for example, ad_lds_instance.fabrikam.com:389.

Domain

Domain

The DNS name of the domain, for example, sales.corp.fabrikam.com.

Forest

Forest

The DNS name of the forest, for example, corp.fabrikam.com.

Application Partition

ApplicationPartition

The DNS name of the application partition.

AD LDS Configuration Set

ConfigurationSet

One of the keywords that is associated with the service connection point registered by AD LDS instances for the configuration set.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
PowerShell DirectoryContext Example

I was trying to get information about a domain that I have an external trust with, and all the examples showed ::GetCurrentDomain() and so on. So after some poking I found how to pass in a normal domain name (Company or company.com) using the DirectoryContext

#$ContextType = "Domain"
$TargetDomain = "company"
$DomainContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain",$TargetDomain)
$ForestContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("Forest",$TargetDomain)
$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($DomainContext)
$Forest = [system.directoryservices.activedirectory.Forest]::GetForest($ForestContext)

In this example instead of setting a variable for my context type, I simply pass in the text of what I want.

 

Domain format can also be NT domain name
Depsite what the documentation here says, if you are creating a DirectoryContext instance that represents a Domain, you can pass in the domain name in the old style NT4 format (e.g MYDOMAIN) , it does not have to be a DNS domain name (e.g mydomain.local).