Active Directory 列表 OU

发布于 2024-09-02 17:19:32 字数 1057 浏览 5 评论 0原文

我当前有这段代码,

        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=gtp,DC=Local 但目录搜索器没有找到任何结果。有人可以帮忙吗?

I have this code currently,

        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();
            }
        }

When i use the debugger i can see that rootDSE.Path is infact pointing to the right place, in this case DC=g-t-p,DC=Local but the directory searcher doesn't find any results. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-09-09 17:19:32

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

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

因此请尝试使用过滤器 < code>(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 属性后需要什么......

Stephen - my bad - for some reason, the search using objectCategory doesn't work.

Even though the objectCategory is displayed as CN=Organizational-Unit, for searching, you still need to use the same value as for the objectClass:

So try to use the filter (objectCategory=organizationalUnit) - that definitely works for me!

UPDATE: in order to get some properties in your search result (in order to display them in the combo box), you need to include those when you create the 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 ...

With this, you should definitely be able to grab the temp.Properties["name"][0] and stick it into the combobox's list of items.

I don't really see what you need the line

DirectoryEntry ou = temp.GetDirectoryEntry();

after grabbing the name property .....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文