我正在尝试从活动目录中获取OU的列表。不幸的是,即使我知道“myApp”域组件中有2个OU,我的搜索也总是没有任何结果。
using (var entry = new DirectoryEntry("LDAP://myServer:1111/DC=myApp,DC=myDomain,DC=com", Username, Password)) {
using (var searcher = new DirectorySearcher()) {
searcher.SearchRoot = entry;
searcher.Filter = "(objectCategory=Organizational-Unit)";
searcher.PropertiesToLoad.Add("name");
//foo never gets results. :(
var foo = searcher.FindAll();
}
}
我尝试按照之前的StackOverflow question中的代码进行操作,但是没有运气。
答案 0 :(得分:1)
我使用这样的东西。它使用路径检索字典名称中的所有OU,只需正确更改SearchScope。
public Dictionary<string, string> GetOUInfo(SearchScope eSearchScope)
{
Dictionary<string, string> retValues = new Dictionary<string, string>();
try
{
DirectoryEntry oDirectoryEntry = new DirectoryEntry("LDAP://myServer:1111/DC=myApp,DC=myDomain,DC=com", Username, Password);
DirectorySearcher oDirectorySearcher = new DirectorySearcher(oDirectoryEntry,
"(objectCategory=organizationalUnit)", null, eSearchScope);
SearchResultCollection oSearchResultCollection = oDirectorySearcher.FindAll();
foreach (SearchResult item in oSearchResultCollection)
{
string name = item.Properties["name"][0].ToString();
string path = item.GetDirectoryEntry().Path;
retValues.Add(path, name);
}
}
catch (Exception ex)
{
}
return retValues;
}
答案 1 :(得分:0)
1)您确定基本搜索"DC=myApp,DC=myDomain,DC=com"
吗? "myApp"
是域组件吗?
2)您可以尝试指定搜索范围吗?
searcher.SearchScope = SearchScope.Subtree;
3)"(objectCategory=Organizational-Unit)"
是Active-Directory理解的快捷方式,但实际上objectCategory属性是专有名称(DN),OU的实际值是:CN=Organizational-Unit,CN=Schema,CN=Configuration,domain root DN
。
您可以尝试使用这种搜索OU更常见的过滤器"(objectClas=Organizational-Unit)"
吗?
在命令行上可以试试吗?
C:\temp>ldifde -f c:\temp\out.txt -d "DC=myApp,DC=myDomain,DC=com" -r "(objectClass=organizationalUnit)"
答案 2 :(得分:0)
使用此功能
PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.com", "OU=Marketing,OU=Corporate,DC=mycompany,DC=com");
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach (var group in ps.FindAll())
{
Console.WriteLine(group.DistinguishedName);
}