我查看了DirectoryServices类,它似乎是我需要的,但我似乎无法找到获取组织单位集合所需的类/方法。
你们能提出一些建议吗?
答案 0 :(得分:19)
您需要使用来自DirectorySearcher
的相应System.DirectoryServices
,并且您需要搜索organizationalUnit
AD类(我建议您根据objectCategory
进行搜索单值和索引 - 比使用objectClass
快得多 - 这样的事情:
List<string> orgUnits = new List<string>();
DirectoryEntry startingPoint = new DirectoryEntry("LDAP://DC=YourCompany,DC=com");
DirectorySearcher searcher = new DirectorySearcher(startingPoint);
searcher.Filter = "(objectCategory=organizationalUnit)";
foreach (SearchResult res in searcher.FindAll())
{
orgUnits.Add(res.Path);
}
答案 1 :(得分:4)
List<PlayerBO> source = new List<PlayerBO>();
DirectoryEntry root = new DirectoryEntry("LDAP://app.shgbit.com");
DirectoryEntry gbvision = root.Children.Find("OU=UMP");
DirectorySearcher searcher = new DirectorySearcher(gbvision);
searcher.Filter = "(objectClass=computer)";
int index = 1;
foreach (SearchResult each in searcher.FindAll())
{
var box = each.GetDirectoryEntry();
source.Add(new PlayerBO { Id = index++, Name = box.Properties["name"].Value.ToString(), Description = box.Properties["description"].Value.ToString() });
}
ListViewAD.ItemsSource = new SelectableSource<PlayerBO>(source);
答案 2 :(得分:3)
我知道这个帖子有点陈旧,但我最近创建了一种更有效的方法来操纵DirectoryEntries而不是DirectorySearcher提供和想要分享,因为这是谷歌的最佳结果。此示例基于初始指定的起点复制OU结构。
传递给第一个构造函数的DN路径应采用格式&#34; LDAP:// OU = StartingOU,DC = test,DC = com&#34;
using System.DirectoryServices;
using System.Threading.Tasks;
public class ADTree
{
DirectoryEntry rootOU = null;
string rootDN = string.Empty;
List<ADTree> childOUs = new List<ADTree>();
public DirectoryEntry RootOU
{
get { return rootOU; }
set { rootOU = value; }
}
public string RootDN
{
get { return rootDN; }
set { rootDN = value; }
}
public List<ADTree> ChildOUs
{
get { return childOUs; }
set { childOUs = value; }
}
public ADTree(string dn)
{
RootOU = new DirectoryEntry(dn);
RootDN = dn;
BuildADTree().Wait();
}
public ADTree(DirectoryEntry root)
{
RootOU = root;
RootDN = root.Path;
BuildADTree().Wait();
}
private Task BuildADTree()
{
return Task.Factory.StartNew(() =>
{
object locker = new object();
Parallel.ForEach(RootOU.Children.Cast<DirectoryEntry>().AsEnumerable(), child =>
{
if (child.SchemaClassname.Equals("organizationalUnit"))
{
ADTree ChildTree = new ADTree(child);
lock (locker)
{
ChildOUs.Add(ChildTree);
}
}
});
});
}
}
要构建,您需要做的就是:
ADTree Root = null;
Task BuildOUStructure = Task.Factory.StartNew(() =>
{
ADTree = new ADTree("LDAP://ou=test,dc=lab,dc=net");
});
BuildOUStructure.Wait();
答案 3 :(得分:3)
Jamie提供的代码运行良好。为了回答MacGuyver的问题,为了使用Cast扩展,您需要在代码中包含对System.Linq的引用。我正在使用以下代码填充显示AD环境中OU的TreeView:
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.Collections.Generic;
using System.DirectoryServices.ActiveDirectory;
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
string top = string.Format("LDAP://DC={0}", Domain.GetCurrentDomain().Name.Replace(".", ",DC="));
ADTree tree = null;
Task BuildOUStructure = Task.Factory.StartNew(() =>
{
tree = new ADTree(top);
});
BuildOUStructure.Wait();
foreach(ADTree t in tree.ChildOUs)
{
TreeNode node = new TreeNode(t.RootOU.Path);
treeView1.Nodes.Add(node);
if(t.ChildOUs.Count > 0)
{
AddChildren(node, t);
}
}
}
private void AddChildren(TreeNode parent, ADTree tree)
{
foreach(ADTree t in tree.ChildOUs)
{
TreeNode node = new TreeNode(t.RootOU.Path);
parent.Nodes.Add(node);
if(t.ChildOUs.Count > 0)
{
AddChildren(node, t);
}
}
}
}
public class ADTree
{
DirectoryEntry rootOU = null;
string rootDN = string.Empty;
List<ADTree> childOUs = new List<ADTree>();
public DirectoryEntry RootOU
{
get { return rootOU; }
set { rootOU = value; }
}
public string RootDN
{
get { return rootDN; }
set { rootDN = value; }
}
public List<ADTree> ChildOUs
{
get { return childOUs; }
set { childOUs = value; }
}
public ADTree(string dn)
{
RootOU = new DirectoryEntry(dn);
RootDN = dn;
BuildADTree().Wait();
}
public ADTree(DirectoryEntry root)
{
RootOU = root;
RootDN = root.Path;
BuildADTree().Wait();
}
private Task BuildADTree()
{
return Task.Factory.StartNew(() =>
{
object locker = new object();
Parallel.ForEach(RootOU.Children.Cast<DirectoryEntry>().AsEnumerable(), child =>
{
if (child.SchemaClassName.Equals("organizationalUnit"))
{
ADTree ChildTree = new ADTree(child);
lock (locker)
{
ChildOUs.Add(ChildTree);
}
}
});
});
}
}
}
这是在一个简单的Windows窗体应用程序上,该应用程序具有TreeView(treeview1)和Button(button1)-希望对您有所帮助!
答案 4 :(得分:0)
您是否查看了DirectorySearcher方法?
答案 5 :(得分:0)
这是我的解决方案,它正在运作:
List<string> DisplayedOU = new List<string>();
int step = 0;
string span = "<span style='margin-left:6px;'> -- </span>";
private void getOU2()
{
string strRet = "";
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://uch.ac/OU=ALL,DC=uch,DC=ac", "user", "pass");
// set up directory searcher based on default naming context entry
DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
// SearchScope: OneLevel = only immediate subordinates (top-level OUs);
// subtree = all OU's in the whole domain (can take **LONG** time!)
ouSearcher.SearchScope = SearchScope.Subtree;
// ouSearcher.SearchScope = SearchScope.Subtree;
// define properties to load - here I just get the "OU" attribute, the name of the OU
ouSearcher.PropertiesToLoad.Add("ou");
// define filter - only select organizational units
ouSearcher.Filter = "(objectCategory=organizationalUnit)";
int cnt = 0;
foreach (SearchResult deResult in ouSearcher.FindAll())
{
string temp = deResult.Properties["ou"][0].ToString();
strRet += FindSubOU(deResult.Properties["adspath"][0].ToString(), cnt);
}
Literal1.Text = strRet;
}
private string FindSubOU(string OU_Path, int cnt)
{
string strRet = "";
DirectoryEntry domainRoot = new DirectoryEntry(OU_Path, "user", "pass");
// set up directory searcher based on default naming context entry
DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
// SearchScope: OneLevel = only immediate subordinates (top-level OUs);
// subtree = all OU's in the whole domain (can take **LONG** time!)
ouSearcher.SearchScope = SearchScope.Subtree;
// ouSearcher.SearchScope = SearchScope.Subtree;
// define properties to load - here I just get the "OU" attribute, the name of the OU
ouSearcher.PropertiesToLoad.Add("ou");
// define filter - only select organizational units
ouSearcher.Filter = "(objectCategory=organizationalUnit)";
//adspath
// do search and iterate over results
foreach (SearchResult deResult in ouSearcher.FindAll())
{
string temp = deResult.Properties["ou"][0].ToString();
if (!DisplayedOU.Contains(deResult.Properties["ou"][0].ToString()))
{
string strPerfix = "";
for (int i = 0; i < step; i++)
strPerfix += span;
strRet += strPerfix + ++cnt + ". " + deResult.Properties["ou"][0].ToString() + " ----> " + deResult.Properties["adspath"][0].ToString() + "<br />";
DisplayedOU.Add(deResult.Properties["ou"][0].ToString());
step++;
strRet += FindSubOU(deResult.Properties["adspath"][0].ToString(), cnt);
step--;
}
}
return strRet;
}