我有一個域名myDomain.local
的Active Directory,它下面有一個Distribution Group
包含很多組。
如何讀取(以編程方式)所有這些子組以檢索其名稱列表?
以及如何優化查詢來篩選結果,以便它只檢索所有以Region
結尾的組?
順便說一句,我使用的是C#.Net,ASP.Net和sharepoint,而我對AD沒有經驗。閱讀/過濾分發組的活動目錄的子組?
2
A
回答
1
這是我所做的解決方案;對於那些有興趣的人:
public ArrayList getGroups()
{
// ACTIVE DIRECTORY AUTHENTICATION DATA
string ADDomain = "myDomain.local";
string ADBranchsOU = "Distribution Group";
string ADUser = "Admin";
string ADPassword = "password";
// CREATE ACTIVE DIRECTORY ENTRY
DirectoryEntry ADRoot
= new DirectoryEntry("LDAP://OU=" + ADBranchsOU
+ "," + getADDomainDCs(ADDomain),
ADUser,
ADPassword);
// CREATE ACTIVE DIRECTORY SEARCHER
DirectorySearcher searcher = new DirectorySearcher(ADRoot);
searcher.Filter = "(&(objectClass=group)(cn=* Region))";
SearchResultCollection searchResults = searcher.FindAll();
// ADDING ACTIVE DIRECTORY GROUPS TO LIST
ArrayList list = new ArrayList();
foreach (SearchResult result in searchResults)
{
string groupName = result.GetDirectoryEntry().Name.Trim().Substring(3);
list.Add(groupName);
}
return list;
}
public string getADDomainDCs(string ADDomain)
{
return (!String.IsNullOrEmpty(ADDomain))
? "DC=" + ADDomain.Replace(".", ",DC=")
: ADDomain;
}
2
如果你在.NET 3.5(或可以升級到它),你可以使用System.DirectoryServices.AccountManagement
命名空間中使用此代碼:
// create the "context" in which to operate - your domain here,
// as the old-style NetBIOS domain, and the container where to operate in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "cn=Distribution Group,dc=YourDomain,dc=local");
// define a "prototype" - an example of what you're searching for
// Here: just a simple GroupPrincipal - you want all groups
GroupPrincipal prototype = new GroupPrincipal(ctx);
// define a PrincipalSearcher to find those principals that match your prototype
PrincipalSearcher searcher = new PrincipalSearcher(prototype);
// define a list of strings to hold the group names
List<string> groupNames = new List<string>();
// iterate over the result of the .FindAll() call
foreach(var gp in searcher.FindAll())
{
// cast result to GroupPrincipal
GroupPrincipal group = gp as GroupPrincipal;
// if everything - grab the group's name and put it into the list
if(group != null)
{
groupNames.Add(group.Name);
}
}
是否能滿足您的需求?
有關System.DirectoryServices.AccountManagement
命名空間的詳細信息,請閱讀MSDN雜誌中的Managing Directory Security Principals in the .NET Framework 3.5文章。
相關問題
- 1. 閱讀活動目錄的ACE
- 2. Asp.net活動目錄LDAP:試圖過濾到組級別
- 3. 活動目錄嵌套組
- 4. 活動記錄分組
- 5. 閱讀子目錄與bash
- 6. LDAP活動目錄PHP過濾
- 7. 活動目錄過濾器PHP
- 8. 閱讀子目錄中的文件
- 9. 如何在活動源中包含子目錄過濾器?
- 10. 是活動目錄組的用戶(域以外)部分
- 11. 閱讀目錄
- 12. 活動目錄 - 獲取用戶和組,特定組的成員
- 13. 活動目錄獲取除特殊組以外的所有組
- 14. 從某一組LDAP活動目錄
- 15. javascript活動目錄用戶/組查詢
- 16. 活動目錄組搜索C#
- 17. 活動目錄羣組不被退回
- 18. C#活動目錄組查詢
- 19. 通過活動目錄發佈程序
- 20. 過濾分支篩選出目錄下創建子目錄
- 21. 軌活動記錄分組與範圍
- 22. 閱讀目錄的文件返回空數組
- 23. 如何通過REST api更改Azure訂閱的活動目錄
- 24. 閱讀數組
- 25. 閱讀正確的目錄
- 26. 過濾一組記錄
- 27. 閱讀目錄爲
- 28. iOS閱讀多個子目錄
- 29. LDAP:使用過濾器來避免活動目錄中的子CN
- 30. 如何訂閱從組件發起的活動
謝謝馬克,我還沒有嘗試你的代碼,但無論如何,我會張貼我爲那些對這個話題感興趣的人做的解決方案。非常感謝。 – 2010-07-02 18:11:53