以层次结构顺序获取活动目录中的所有内容

编程入门 行业动态 更新时间:2024-10-21 19:50:24
本文介绍了以层次结构顺序获取活动目录中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想按层次顺序获取域中的所有OU并将其绑定到MVC中的树视图。 我尝试过: 我尝试了下面的代码,但它返回空。

I would like to get all OUs in a domain in hierarchical order and bind it to tree view in MVC. What I have tried: I tried below code but it returns empty.

Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "domainname", "username", "password")); Forest forest = domain.Forest; DomainCollection domains = forest.Domains;

但是如果我使用这段代码,我会得到OU列表但不是我想要的。

But if I use this code I get list of OUs but not how I want.

List<string> lstName = new List<string>(); DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://DC=domainname,DC=com", "username", "password"); DirectorySearcher oDirectorySearcher = new DirectorySearcher(directoryEntry,"(objectCategory=organizationalUnit)", null); SearchResultCollection oSearchResultCollection = oDirectorySearcher.FindAll(); foreach (SearchResult item in oSearchResultCollection) { string name = item.Properties["name"][0].ToString(); lstName.Add(name); }

有人可以帮忙吗? 谢谢

Can anyone help? Thanks

推荐答案

AD中没有搜索功能,或者几乎没有任何系统可以返回层次结构。你必须自己构建它。 在TreeView的根目录中获取你想要的顶级项目。将这些项目添加到每个项目并为其添加一个虚拟项目,以便在它们旁边显示一个小+符号。当用户单击+展开该节点时,您将处理BeforeExpand事件。在那里,您删除虚拟节点,执行另一个查询以从该节点获取所需的子项,并将它们添加到正在扩展的节点。再次,用一个虚拟节点填充子节点以获得+到它们。 这就是它的全部。 There is no search function in AD, or pretty much any system for that matter, that will return a hierarchy. You have to build it yourself. Get the top level items you want in the root of your TreeView. Add those items to and add a dummy item to each of them so you get the little "+" sign next to them. When the use clicks the "+" to expand that node, you handle the BeforeExpand event. In there, you remove the dummy node, do another query to get the child items you want from that node and add them to the node being expanded. Again, populate thee child nodes with a single dummy node to get the "+" to to them. That's all there is to it.

更多推荐

以层次结构顺序获取活动目录中的所有内容

本文发布于:2023-10-16 16:35:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所有内容   顺序   层次   结构   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!