2012-01-02 108 views
3

我建議使用System.DirectoryServices.Protocols以支持連接到Active Directoy here以外的LDAP服務器。
不幸的是,它我沒有能夠正確地搜索目錄。我希望能夠獲得用戶的某個屬性(例如mail)。這很容易通過使用DirectorySearcher類在System.DirectoryServices名稱空間中完成。我如何在System.DirectoryServices.Protocols命名空間中實現相同的功能。這是我到目前爲止有:從.NET連接到LDAP服務器

var domainParts = domain.Split('.'); 
string targetOu = string.Format("cn=builtin,dc={0},dc={1}", domainParts[0], domainParts[1]); 
string ldapSearchFilter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", username); 

// establish a connection to the directory 
LdapConnection connection = new LdapConnection(
           new LdapDirectoryIdentifier(domain), 
           new NetworkCredential() { UserName = username, 
                Password = "MyPassword" }); 
SearchRequest searchRequest = new SearchRequest(
       targetOu, ldapSearchFilter, SearchScope.OneLevel, new[] {"mail"}); 

此代碼引發DirectoryOperationException類型的異常與消息The object does not exist

我懷疑我的targetOuldapSearchFilter變量有問題。

謝謝。

+0

這是違反非Microsoft LDAP? – 2012-01-02 11:16:34

+0

它應該同時支持(例如OpenLDAP)。 – Kamyar 2012-01-02 11:18:11

回答

3

我懷疑主要問題可能是:samAccountName是嚴格限制Windows的屬性,其他LDAP服務器不知道。因此,如果您要違反非Active Directory LDAP,則應該使用其他方法進行搜索 - 例如, sn(用於姓氏或姓氏),givenName(名字),可能是displayName

另一個有趣的選擇可能是使用ANR(模棱兩可的名稱解析)搜索 - 請參閱大致位於中間的這個page on SelfADSI,其中解釋ANR。

有了ANR,你會寫你的查詢是這樣的:

string ldapSearchFilter = 
    string.Format("(&(ObjectCategory={0})(anr={1}))", "person", username); 

我也改變ObjectClassObjectCategory的原因有兩個:

  • ObjectCategory是單值,例如只包含一個值(ObjectClass是多值)
  • ObjectCategory通常索引,因此搜索通常使用速度快了很多ObjectCategory

這是否返回你正在尋找的結果?

+0

感謝您的快速回復。我改變了過濾器以使用'anr'和'ObjectCategory'。例外消失了。但'searchResponse.Entries'有0個元素。我現在正在查詢AD服務。如果我使用'System.DirectoryServices',我可以正確地獲得郵件屬性。 – Kamyar 2012-01-02 11:34:29

+0

@Kamyar:你可以在S.DS中針對OpenLDAP運行查詢嗎? – 2012-01-02 11:37:08

+0

不,不幸的是我無法訪問OpenLDAP服務器。但是使用'System.DirectoryServices.Protocols'並在AD中產生結果是一個很好的步驟。我可以稍後測試OpenLDAP。 – Kamyar 2012-01-02 11:40:27