问题描述
我们想根据多个 OU 查询特定部门用户的 Active Directory。
给定代码:
List<string> orgUnitsList= new List<string>(){"Banking","Support","Finance"};
List<ADUserInfo> adUsers = new List<ADUserInfo>();
foreach (var ou in orgUnitsList)
{
// Construct the LDAP path and create instance of DirectorySearcher
var ldap = $"LDAP://mydomain.local/OU={ou},DC=mydomain,DC=local";
DirectoryEntry directoryEntry = new DirectoryEntry(ldap);
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
// Add PropertiesToLoad (for speeding up things)
foreach (var propery in ADUserInfo.QueryProperties)
directorySearcher.PropertiesToLoad.Add(property);
// Set the filter and trigger FindAll()
directorySearcher.Filter = "(&(objectClass=User)(department=" + searchString + "*))";
SearchResultCollection searchResultCollection = directorySearcher.FindAll();
Console.WriteLine($"{searchResultCollection.Count} users found for OU: {ou}");
// Adding results to the returning list
foreach (SearchResult u in searchResultCollection)
{
var user = new ADUserInfo(u);
adUsers.Add(user);
}
}
return adUsers;
这种方法简单吗? 我们做错了什么? 将 OU 添加到过滤器是为了加快查询速度,因为查询需要很长时间(超过一分钟并达到超时),同样通过添加 PropertiesToLoad..
directorySearcher.Filter = "(&(objectClass=User)(|(displayName=" + searchString + "*)))";
感谢任何帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)