gpt4 book ai didi

c# - Active Directory 列表 OU

转载 作者:太空狗 更新时间:2023-10-29 21:25:21 25 4
gpt4 key购买 nike

我目前有这段代码,

        string defaultNamingContext;

DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
rootDSE = new DirectoryEntry("LDAP://" + defaultNamingContext);
//DirectoryEntry domain = new DirectoryEntry((string)"LDAP://" + defaultNamingContext);

DirectorySearcher ouSearch = new DirectorySearcher(rootDSE,"(objectCategory=Organizational-Unit)",
null, SearchScope.Subtree);

MessageBox.Show(rootDSE.Path.ToString());
try
{
SearchResultCollection collectedResult = ouSearch.FindAll();
foreach (SearchResult temp in collectedResult)
{
comboBox1.Items.Add(temp.Properties["name"][0]);
DirectoryEntry ou = temp.GetDirectoryEntry();
}
}

当我使用调试器时,我可以看到 rootDSE.Path 实际上指向了正确的位置,在本例中是 DC=g-t-p,DC=Local 但目录搜索器没有找到任何结果.谁能帮忙?

最佳答案

斯蒂芬 - 我的错 - 由于某种原因,使用 objectCategory 的搜索不起作用。

即使 objectCategory 显示为 CN=Organizational-Unit,要进行搜索,您仍然需要使用与 objectClass 相同的值:

所以尝试使用过滤器 (objectCategory=organizationalUnit) - 这绝对适合我!

更新:为了在您的搜索结果中获得一些属性(以便在组合框中显示它们),您需要在创建 DirectorySearcher 时包含这些属性:

DirectorySearcher ouSearch = new DirectorySearcher(rootDSE);
ouSearch.Filter = "(objectCategory=Organizational-Unit)";
ouSearch.SearchScope = SearchScope.Subtree;

ouSearch.PropertiesToLoad.Add("name");
// add more properties if you want to ...

有了这个,您绝对应该能够获取 temp.Properties["name"][0] 并将其粘贴到组合框的项目列表中。

我真的不明白你需要什么线

DirectoryEntry ou = temp.GetDirectoryEntry();

抓取name属性后.....

关于c# - Active Directory 列表 OU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2906172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com