我被推荐使用System.DirectoryServices.Protocols
能够支持连接到 Active Directory 以外的 LDAP 服务器here.
不幸的是,我无法正确搜索该目录。我希望能够获得用户的某个属性(例如mail
)。这很容易完成System.DirectoryServices
命名空间通过使用DirectorySearcher
班级。我怎样才能实现同样的目标System.DirectoryServices.Protocols
命名空间。这是我到目前为止所拥有的:
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
and ldapSearchFilter
变量。
Thanks.
我怀疑主要问题可能是:samAccountName
是严格仅限 Windows 的属性,其他 LDAP 服务器不会知道。
因此,如果您要使用非 Active Directory LDAP,则应该使用其他内容进行搜索 - 例如sn
(姓氏或姓氏),givenName
(名字),可能displayName
.
另一个有趣的选择可能是使用 ANR(模糊名称解析)搜索 - 请参阅此SelfADSI 页面大致在中间,解释了 ANR。
使用 ANR,您可以像这样编写查询:
string ldapSearchFilter =
string.Format("(&(ObjectCategory={0})(anr={1}))", "person", username);
我也改变了ObjectClass
to ObjectCategory
有两个原因:
-
ObjectCategory
是单值的,例如仅包含单个值(ObjectClass
是多值的)
-
ObjectCategory
通常会被索引,因此使用搜索通常会快得多ObjectCategory
这会返回您正在寻找的结果吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)