我正在尝试从active directory获取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中的代码,但没有成功。
发布于 2014-03-13 01:51:20
我使用类似这样的东西。它使用路径检索字典名称中所有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;
}发布于 2013-04-04 12:04:27
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。
您是否可以尝试使用更常用的筛选器"(objectClas=Organizational-Unit)"来搜索OU?
在命令行中,您可以尝试这样做吗?
C:\temp>ldifde -f c:\temp\out.txt -d "DC=myApp,DC=myDomain,DC=com" -r "(objectClass=organizationalUnit)"发布于 2017-03-02 13:29:59
用这个它会起作用的
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);
}https://stackoverflow.com/questions/15797387
复制相似问题