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

ksc*_*ott 7 ldap active-directory

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

任何作为实际肉体和骨骼人员的用户帐户都将放入为其工作的办公室命名的OU中作为其主要OU.任何别名,通用帐户或不直接绑定到真实用户的用户帐户都将"主OU"OU设置为其主要OU.

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

我的任务是编写一个只能获得所有实际肉体和骨骼人的查询.

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

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

mar*_*c_s 11

是的,当然 - 你需要:

1)绑定到特定的OU

DirectoryEntry myOU = new DirectoryEntry("LDAP://OU=MyOU,......,DC=MyCompany,DC=com");
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

4)返回该列表


Jos*_*a G 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();
Run Code Online (Sandbox Code Playgroud)