public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //添加GetOuTree的内部Operate初始化参数 //Add the internal Operate initialization parameter of GetOuTree string domainip = context.Session["domainip"].ToString(); string domainname = context.Session["domainname"].ToString(); string username = context.Session["username"].ToString(); string password = context.Session["password"].ToString(); string dc = context.Session["dc"].ToString(); JObject jobject = JObject.FromObject(OuTreeNode.GetOuTree(domainname, username, password, domainip, dc)); JArray jarray = new JArray(); jarray.Add(jobject); string json = jarray.ToString(); context.Response.Write(json); }
public static OuTreeNode GetOuTree(string domainname, string username, string password, string domainip, string dc) { Operate op = new Operate(domainname, domainip, username, password, dc); string domainName = string.Empty; OuTreeNode rootNode = null; DirectoryContextType type = new DirectoryContextType(); DirectoryContext text = new DirectoryContext(type, op.adminUser, op.adminPwd); using (Domain domain = Domain.GetDomain(text)) { domainName = domain.Name; //使用域节点来构造OU树的根节点 Use the domain node to construct the root node of the OU tree rootNode = new OuTreeNode() { text = domainName }; //递归的查找子节点,构造OU树 Recursively finds the child nodes and constructs the OU tree GetOuTreeRecursivly(rootNode, domain.GetDirectoryEntry()); } return(rootNode); }
//递归构造树节点 Recursive construction tree node private static void GetOuTreeRecursivly(OuTreeNode parentNode, DirectoryEntry parentDirectoryEntry) { using (DirectorySearcher ds = new DirectorySearcher(parentDirectoryEntry)) { ds.Filter = "(objectClass=organizationalunit)"; ds.SearchScope = SearchScope.OneLevel; //仅查找当前OU下的子OU Just search the child OU under the current OU try { using (SearchResultCollection result = ds.FindAll()) { foreach (SearchResult entry in result) { //获取Name属性 Get Name attribute string name = entry.GetDirectoryEntry().Properties["Name"].Value.ToString(); byte[] bytes = entry.GetDirectoryEntry().Properties["ObjectGuid"].Value as byte[]; Guid id = new Guid(bytes); //获取id属性 Get id attribute OuTreeNode node = new OuTreeNode() { text = name, id = id.ToString() }; parentNode.nodes.Add(node); using (DirectoryEntry child = entry.GetDirectoryEntry()) { GetOuTreeRecursivly(node, child); } } } } catch (Exception e) { throw e; } } }