绑定字符串

下面的示例说明如何绑定到目录中不同类型的对象。本主题中的绑定字符串使用 LDAP 服务提供程序的语法,该提供程序用于绑定到 Active Directory 和其他 LDAP 目录对象。绑定字符串称为 ADsPath。

绑定到当前域

对于大多数的客户端应用程序,绑定到为用户提供身份验证的域。下面的示例说明此类型的绑定。

Dim ent As New DirectoryEntry()
DirectoryEntry ent = new DirectoryEntry();

绑定到特定服务器

下面的示例说明如何通过将服务器名称添加到 ADsPath 来绑定到特定服务器。

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

绑定到特定域

下面的示例说明如何将域名添加到 ADsPath 来绑定到特定域。

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

绑定到特定对象

若要从特定对象修改或读取数据,请通过将其相对可分辨名称 (RDN) 添加到绑定字符串来绑定到该对象。在下面的示例中,绑定点位于用户对象 Jeff Smith 上。

' 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")
// 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");

使用替代凭据进行绑定

下面的示例说明如何将从用户界面传递的用户名和密码用作凭据来绑定到服务器。

' 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)
// GetUserNameFromUI() and GetPasswordFromUI() are functions created to pass in data.
String userName = GetUserNameFromUI();
string password = GetPasswordFromUI();
DirectoryEntry ent = new DirectoryEntry("LDAP://server01", userName, password);

使用标志进行绑定

下面的示例说明如何使用 AuthenticationType 属性指定替代绑定选项。在 .NET Framework 2.0 之前,AuthenticationType 的默认值为 None。从 .NET Framework 2.0 开始,默认值为 Secure

Dim ent As New DirectoryEntry("LDAP://server01", Nothing, Nothing, AuthenticationTypes.ServerBind Or AuthenticationTypes.FastBind)
DirectoryEntry ent = new DirectoryEntry("LDAP://server01",null,null,AuthenticationTypes.ServerBind | AuthenticationTypes.FastBind);

若要在应用程序脱离范围之前清除内存,请对绑定的对象调用 Dispose 方法。

另请参见

参考

System.DirectoryServices
DirectoryEntry
AuthenticationTypes

概念

绑定到目录对象

Send comments about this topic to Microsoft.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。