我正在使用 C# 中的以下代码搜索 LDAP,以轮询用户的活动目录:
DirectoryEntry entry = new DirectoryEntry(ldapPath, userName, password);
DirectorySearcher Searcher = new DirectorySearcher(entry);
Searcher.CacheResults = true;
Searcher.SearchScope = SearchScope.Subtree;
Searcher.Filter = "(&(&(objectCategory=person)(objectClass=user))
(|(samaccountname=" + userSearch.SamAccountName + "*)
(&(GivenName=" + userSearch.FirstName + "*)(SN=" + userSearch.Surname +
"*))))";
Searcher.PropertiesToLoad.AddRange(new string[] {"DisplayName", "GivenName",
"DistinguishedName","Title","manager",
"mail", "physicalDeliveryOfficeName", "DirectReports", "Company",
"Description", "SAMAccountName"});
SearchResultCollection results = Searcher.FindAll();
List<ActiveUser> activeUsers = new List<ActiveUser>();
我使用输入参数 userSearch.FirstName = "jo" 和 userSearch.LastName = "bl" 运行它,并期待一个用户“Joe Bloggs”,但这没有出现在结果列表中。如果我使用 Windows 中的 Active Directory 用户和计算机工具中的名称文本框尝试执行此操作,Joe Bloggs 将显示为列表中的唯一用户。我正在使用正确的 LDAP 路径。我是否使用了错误的过滤器来复制 Windows 工具中的功能?是否有针对显示名称的“类似”搜索?
任何帮助,将不胜感激。
如果您使用的是 .NET 3.5 或更高版本,则可以使用PrincipalSearcher
和一个“按示例查询”主体来进行搜索:
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Jo*";
qbeUser.Surname = "Bl*";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
如果您还没有——请务必阅读 MSDN 文章管理 .NET Framework 3.5 中的目录安全主体 http://msdn.microsoft.com/en-us/magazine/cc135979.aspx它很好地展示了如何充分利用新功能System.DirectoryServices.AccountManagement
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)