LDAP查询特定OU中子OU中的所有用户

时间:2009-06-26 16:27:17

标签: active-directory ldap

我必须处理的活动目录如下:域包含许多OU。其中一个OU被命名为“Primary OU”。在这个OU中有几个以全球办事处所在地命名的OU(即“芝加哥”“巴黎”)。

任何作为实际骨肉人的用户帐户都会被放入为其工作的办公室命名的OU中作为其主要OU。任何别名,通用帐户或其他未直接绑定到真实用户的用户帐户都将“主OU”OU设置为其主要OU。

数据方面,这种主要的OU区别是唯一指示哪些用户是真人,哪些用户不是真人。没有任何组只包含真实的人,任何领域都没有指示他们是真实的人,并且严格禁止对活动目录或任何用户帐户进行任何更改。

我的任务是撰写一个只会让所有肉体和骨骼都受到影响的查询。

不幸的是,LDAP并不是我的强项,我想出的唯一方法就是单独搜索每个办公室子OU并将所有结果放在一起,但是有很多办公室,需要更改查询是否添加了任何办公室,我需要避免。

有没有办法查询特定OU的“子”OU中的所有用户,但不能直接在父OU中返回任何用户?

2 个答案:

答案 0 :(得分:11)

是的,确定 - 你需要:

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)返回该列表

马克

答案 1 :(得分:6)

// 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();
相关问题