DirectorySearcher 不从 LDAP 查询返回任何结果 (C#)
DirectorySearcher not returning any results from LDAP query (C#)
我正在尝试 return 2 个 OU 中的所有用户。第一个 OU(下方)是 HSD 用户 OU 中的 IT 用户。这 return 每次都是空的,但我可以通过以下方式让所有用户恢复。
search.Filter = "(&(objectClass=user))";
我尝试了 OU 和 DC 的多种变体组合,但没有结果。
string DomainPath = "LDAP://hs.domain.org";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
//The following filter does work
//search.Filter = "(&(objectClass=user))";
search.Filter = string.Format("(&(objectClass=user)(OU=IT Users,OU=HSD Users,DC=hs,DC=domain,DC=org)");
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("usergroup");
search.PropertiesToLoad.Add("displayname");//first name
search.PropertiesToLoad.Add("manager");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
您使用的 LDAP 过滤器无效,因此没有返回任何记录。要在特定 container/OU 或子树中定位对象,您需要将 searchRoot 设置为路径。
要查找直接包含在特定 container/OU 中的所有条目,请使用以下构造函数将 SearchScope 设置为 1 (OneLevel):
DirectorySearcher(DirectoryEntry, String, String[], SearchScope)
如果您想在 下找到所有匹配的条目 一个特定的 container/OU,那么您可以使用上面的构造函数,但使用特定的 container/OU 作为您的搜索根。
我正在尝试 return 2 个 OU 中的所有用户。第一个 OU(下方)是 HSD 用户 OU 中的 IT 用户。这 return 每次都是空的,但我可以通过以下方式让所有用户恢复。
search.Filter = "(&(objectClass=user))";
我尝试了 OU 和 DC 的多种变体组合,但没有结果。
string DomainPath = "LDAP://hs.domain.org";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
//The following filter does work
//search.Filter = "(&(objectClass=user))";
search.Filter = string.Format("(&(objectClass=user)(OU=IT Users,OU=HSD Users,DC=hs,DC=domain,DC=org)");
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("usergroup");
search.PropertiesToLoad.Add("displayname");//first name
search.PropertiesToLoad.Add("manager");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
您使用的 LDAP 过滤器无效,因此没有返回任何记录。要在特定 container/OU 或子树中定位对象,您需要将 searchRoot 设置为路径。
要查找直接包含在特定 container/OU 中的所有条目,请使用以下构造函数将 SearchScope 设置为 1 (OneLevel):
DirectorySearcher(DirectoryEntry, String, String[], SearchScope)
如果您想在 下找到所有匹配的条目 一个特定的 container/OU,那么您可以使用上面的构造函数,但使用特定的 container/OU 作为您的搜索根。