繫結字串

下列範例示範如何繫結至目錄中不同的物件類型。此主題中的繫結字串使用 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.

Copyright © 2007 by Microsoft Corporation.All rights reserved.