我有一個域名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. 活動記錄分組
- 2. 活動目錄過濾器PHP
- 3. LDAP活動目錄PHP過濾
- 4. 如何在活動源中包含子目錄過濾器?
- 5. 活動目錄獲取除特殊組以外的所有組
- 6. 從某一組LDAP活動目錄
- 7. C#活動目錄組查詢
- 8. 通過活動目錄發佈程序
- 9. 過濾分支篩選出目錄下創建子目錄
- 10. 閱讀目錄的文件返回空數組
- 11. 閱讀數組
- 12. 閱讀正確的目錄
- 13. 閱讀目錄爲
- 14. LDAP:使用過濾器來避免活動目錄中的子CN
- 15. iOS閱讀多個子目錄
- 16. 如何訂閱從組件發起的活動
- 17. jqGrid過濾分組狀態
- 18. Crossfilter分組過濾鍵
- 19. 使用ASP.NET 2中的活動目錄組登錄表單
- 20. 在另一個活動記錄上過濾活動記錄
- 21. 數組中的數組閱讀
- 22. 將文件組移動到子目錄
- 23. 將羣組添加到羣組(活動目錄)Delphi
- 24. 運行git過濾分支子目錄過濾器還去除擺脫其他子目錄歷史
- 25. 離子如何閱讀JSON數組
- 26. LinkedIn API,閱讀羣組帖子問題
- 27. sapui5中的過濾器分組
- 28. RxJS可觀察的過濾和分組
- 29. 過濾分組條形圖的數據
- 30. 過濾列表反應的組分
謝謝馬克,我還沒有嘗試你的代碼,但無論如何,我會張貼我爲那些對這個話題感興趣的人做的解決方案。非常感謝。 – 2010-07-02 18:11:53