我建議使用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
。
我懷疑我的targetOu
和ldapSearchFilter
變量有問題。
謝謝。
這是違反非Microsoft LDAP? – 2012-01-02 11:16:34
它應該同時支持(例如OpenLDAP)。 – Kamyar 2012-01-02 11:18:11