問題描述
我想使用 AccountManagement 列出組織單位中的所有組.
I'd like to use AccountManagement to list all the groups in an Organizational Unit.
以下代碼段適用于 DirectoryServices,但我必須在結果中使用 DirectoryEntry 路徑實例化 GroupPrincipal(這感覺像是一個骯臟的修復).
The following snippet works with DirectoryServices but I would have to instanciate GroupPrincipal with the DirectoryEntry path in the result (which feels like a dirty fix).
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 并使用 PrincipalSearcher
-System.DirectoryService.AccountManagement
中的類來完成你需要的,像這樣:
You can set the PrincipalContext
to the OU where you want to start the search and use the PrincipalSearcher
-class in System.DirectoryService.AccountManagement
to accomplish what you need, like this:
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();
這篇關于使用 DirectoryServices.AccountManagement 從 OU 獲取組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!