Binding Strings

The following code examples show how to bind to different types of objects in the directory. The binding strings in this topic use the syntax for an LDAP service provider, which is used to bind to Active Directory and other LDAP directory objects. The binding string is called an ADsPath.

Binding to the current domain

For most client applications, bind to the domain that provides authentication for the user. The following code examples show this type of binding.

[Visual Basic .NET]

Dim ent As New DirectoryEntry()

[C#]

DirectoryEntry ent = new DirectoryEntry();

Binding to a specific server

The following code examples show how to bind to a specific server by adding the server name to your ADsPath.

[Visual Basic .NET]

Dim ent As New DirectoryEntry("LDAP://server01")

[C#]

DirectoryEntry ent = new DirectoryEntry("LDAP://server01");

Binding to a specific domain

The following code examples show, as with the server name, how to add the domain name to the ADsPath to bind to a specific domain.

[Visual Basic .NET]

Dim ent As New DirectoryEntry("LDAP://platform.fabrikam.com")

[C#]

DirectoryEntry ent = new DirectoryEntry("LDAP://platform.fabrikam.com");

Binding to a specific object

To modify or read data from a specific object, bind to that object by adding its relative distinguished name (RDN) to the binding string. In the following code examples, the binding point is on the user object Jeff Smith.

[Visual Basic .NET]

' If the object is on the domain that you are connected to, use this statment.
Dim ent As New DirectoryEntry("LDAP://CN=Jeff Smith,OU=Marketing,DC=fabrikam,DC=Com")
' If you know the server where the object is located, and you want to reduce search hits, use this statement.
Dim ent As New DirectoryEntry("LDAP://server01/CN=Jeff Smith,OU=Marketing,DC=fabrikam,DC=Com")
' To search a specific domain for this object, use this statement.
Dim ent As New DirectoryEntry("LDAP://fabrikam.com/CN=Jeff Smith,OU=Marketing,DC=fabrikam,DC=Com")

[C#]

// If the object is on the domain that you are connected to, use this statment.
DirectoryEntry ent = new DirectoryEntry("LDAP://CN=Jeff Smith,OU=Marketing,DC=fabrikam,DC=Com");
// If you know the server where the object is located, to reduce search hits, use this statement.
DirectoryEntry ent = new DirectoryEntry("LDAP://server01/CN=Jeff Smith,OU=Marketing,DC=fabrikam,DC=Com");
// To search a specific domain for this object, use this statement.
DirectoryEntry ent = new DirectoryEntry("LDAP://fabrikam.com/CN=Jeff Smith,OU=Marketing,DC=fabrikam,DC=Com");

Binding using an alternate credential

The following code example uses a user name and password passed from a user interface as credentials to bind to a server.

[Visual Basic .NET]

' GetUserNameFromUI() and GetPasswordFromUI() are functions created to pass in data.
Dim userName As [String] = GetUserNameFromUI()
Dim password As String = GetPasswordFromUI()
Dim ent As New DirectoryEntry("LDAP://server01", userName, password)

[C#]

// GetUserNameFromUI() and GetPasswordFromUI() are functions created to pass in data.
String userName = GetUserNameFromUI();
string password = GetPasswordFromUI();
DirectoryEntry ent = new DirectoryEntry("LDAP://server01",userName,password);

Binding using flags

For more information and a list of flags used to bind to objects in the System.DirectoryServices namespace, see AuthenticationTypes Enumeration.

[Visual Basic .NET]

Dim ent As New DirectoryEntry("LDAP://server01", Nothing, Nothing, AuthenticationTypes.ServerBind Or AuthenticationTypes.FastBind)

[C#]

DirectoryEntry ent = new DirectoryEntry("LDAP://server01",null,null,AuthenticationTypes.ServerBind | AuthenticationTypes.FastBind);

If you have used ADSI, you may know that, in C++ applications, you must release directory objects that you are bound to when you are finished with them. This operation is not performed in System.DirectoryServices because the garbage collector cleans up the object when it leaves the scope of an operation. To clear the memory before the application leaves the scope, call the Dispose method on the bound object; that is, the DirectoryEntry.