2009-06-26 104 views
5

我必須處理的活動目錄的佈局如下:該域包含許多OU。其中一個OU被命名爲「主OU」。在這個OU中有幾個OU以全球辦事處的位置命名(即「芝加哥」,「巴黎」)。針對特定OU內的子OU中的所有用戶的LDAP查詢

任何真正的骨肉人的用戶帳戶都放入他們工作的辦公室的OU中,作爲他們的主要OU。任何作爲別名,通用帳戶或不直接綁定到真人的用戶帳戶都將「主OU」OU設置爲其主OU。

從數據的角度來看,這個主要的OU區別是唯一指出哪些用戶是真實的人,哪些用戶不是。沒有隻包含真實人員的組,任何字段中都沒有指示他們是真實的人,並且對活動目錄或任何用戶帳戶進行任何更改都是嚴格禁止的。

我的任務是寫一個查詢,只會得到所有實際的肉骨頭人。

不幸的是,LDAP並不完全是我的強項,我想出的唯一辦法是分別搜索這些辦公室子OU中的每一個,並將所有結果放在一起,但有很多辦公室,它需要如果添加了任何辦公室,請更改爲查詢,這是我需要避免的。

有沒有辦法查詢特定OU的「子」OU中的所有用戶,但不直接返回父OU中的任何用戶?

+0

什麼編程環境? .NET? .NET 2.0或.NET 3.5? – 2009-06-26 16:29:49

+0

我正在查找LDAP查詢,但我最終將使用.NET 3.5中的DirectorySearcher對象執行查詢 – kscott 2009-06-26 16:32:34

回答

9

是的,沒錯 - 你將需要:

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

馬克

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();