簡體   English   中英

使用DirectoryServices.AccountManagement從OU獲取組

[英]Get Groups From OU using DirectoryServices.AccountManagement

我想使用AccountManagement列出組織單位中的所有組。

以下代碼段與DirectoryServices一起使用,但我必須在結果中使用DirectoryEntry路徑實現GroupPrincipal(感覺就像一個臟修復)。

DirectoryEntry root = new DirectoryEntry("LDAP://OU=Marketing,OU=Operations,OU=Applications,DC=mycompany,DC=local")
        DirectorySearcher ds = new DirectorySearcher(root);
        ds.Filter = "(objectCategory=group)";
        SearchResultCollection results = ds.FindAll();

有人有想法嗎?

謝謝!

您可以將PrincipalContext設置為要開始搜索的OU,並使用System.DirectoryService.AccountManagementPrincipalSearcher -class來完成所需的操作,如下所示:

PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.local", "OU=Marketing,OU=Operations,OU=Applications,DC=mycompany,DC=local");
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach(var group in ps.FindAll())
{
  Console.WriteLine(group.DistinguishedName);
}
Console.ReadLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM