针对特定OU内的子OU中的所有用户的LDAP查询
问题描述:
我必须处理的活动目录的布局如下:该域包含许多OU。其中一个OU被命名为“主OU”。在这个OU中有几个OU以全球办事处的位置命名(即“芝加哥”,“巴黎”)。针对特定OU内的子OU中的所有用户的LDAP查询
任何真正的骨肉人的用户帐户都放入他们工作的办公室的OU中,作为他们的主要OU。任何作为别名,通用帐户或不直接绑定到真人的用户帐户都将“主OU”OU设置为其主OU。
从数据的角度来看,这个主要的OU区别是唯一指出哪些用户是真实的人,哪些用户不是。没有只包含真实人员的组,任何字段中都没有指示他们是真实的人,并且对活动目录或任何用户帐户进行任何更改都是严格禁止的。
我的任务是写一个查询,只会得到所有实际的肉骨头人。
不幸的是,LDAP并不完全是我的强项,我想出的唯一办法是分别搜索这些办公室子OU中的每一个,并将所有结果放在一起,但有很多办公室,它需要如果添加了任何办公室,请更改为查询,这是我需要避免的。
有没有办法查询特定OU的“子”OU中的所有用户,但不直接返回父OU中的任何用户?
答
是的,没错 - 你将需要:
1)绑定到特定的OU
DirectoryEntry myOU = new DirectoryEntry("LDAP://OU=MyOU,......,DC=MyCompany,DC=com");
2)枚举所有子OU的
DirectorySearcher subOUsearcher = new DirectorySearcher(myOU);
subOUsearcher.SearchScope = SearchScope.OneLevel; // don't recurse down
subOUsearcher.Filter = "(objectClass=organizationalUnit)";
foreach(SearchResult subOU in subOUsearcher.FindAll())
{
// stick those Sub OU's into a list and then handle them
}
3)单逐一列举每个子OU中的所有用户并将其粘贴到全球用户列表中
DirectorySearcher userSearcher = new DirectorySearcher(myCurrentSubOu);
userSearcher.SearchScope = SearchScope.OneLevel; // don't recurse down
userSearcher.Filter = "(objectClass=user)";
foreach(SearchResult user in userSearcher.FindAll())
{
// stick those users into a list being built up
}
4)返回列表
马克
答
// Create a new DirectorySearcher that starts at the root.
// You can start it anywhere you want though
// by providing a value in the DirectoryEntry constructor.
DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry());
// Set the scope to Subtree in order to search all children.
searcher.SearchScope = SearchScope.Subtree;
// Set the filter to only look for Organizational Units
// that have the name you are looking for.
searcher.Filter = "(&(objectClass=organizationalUnit)(name=" + ouName + "))";
// If you are looking for only one result then do the following two things.
SearchResult result = searcher.FindOne();
DirectoryEntry newDir = result.GetDirectoryEntry();
什么编程环境? .NET? .NET 2.0或.NET 3.5? – 2009-06-26 16:29:49
我正在查找LDAP查询,但我最终将使用.NET 3.5中的DirectorySearcher对象执行查询 – kscott 2009-06-26 16:32:34