活动目录列表OU的目录、列表、OU

2023-09-08 12:43:25 作者:缘ジ梦似梦

我有这个code目前,

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

当我使用调试器,我可以看到,rootDSE.Path是逸岸指向正确的地方,在这种情况下, DC = GTP,DC =本地但目​​录搜索没有找到任何结果。任何人都可以帮忙吗?

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?

推荐答案

斯蒂芬 - 我的坏 - 由于某种原因,使用objectCategory属性的搜索无法正常工作

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

尽管 objectCategory属性显示为 CN =组织单位,用于搜索,你仍然需要使用相同的值作为对象类:

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

因此​​,试图使用过滤器(objectCategory属性=组织单位)! - 这肯定对我的作品

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

更新:,以获得在搜索结果的一些属性(以显示它们在组合框中),你需要在创建DirectorySearcher从包括这些:

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 ...

有了这个,你绝对应该能够抓住 temp.Properties [名称] [0] ,并把它贴到项目的组合框的列表中。

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

抓住name属性后......

after grabbing the name property .....