C# 读取/筛选通讯组';组active directory的子组?

C# 读取/筛选通讯组';组active directory的子组?,c#,sharepoint,active-directory,distribution,active-directory-group,C#,Sharepoint,Active Directory,Distribution,Active Directory Group,我有一个Active Directory,其域为myDomain.local,下面有一个通讯组,其中包含许多组。 如何(以编程方式)读取所有这些子组以检索它们的名称列表 以及如何优化查询以过滤结果,从而只检索以单词Region结尾的所有组? 顺便说一句,我使用的是C#.Net、ASP.Net和sharepoint,我对广告没有经验。如果您使用的是.Net 3.5(或可以升级到它),您可以使用System.DirectoryServices.AccountManagement命名空间使用此代码:

我有一个Active Directory,其域为
myDomain.local
,下面有一个
通讯组,其中包含许多组。
如何(以编程方式)读取所有这些子组以检索它们的名称列表
以及如何优化查询以过滤结果,从而只检索以单词
Region
结尾的所有组?
顺便说一句,我使用的是C#.Net、ASP.Net和sharepoint,我对广告没有经验。

如果您使用的是.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);
    }
}
//创建要在其中操作的“上下文”-此处是您的域,
//作为旧式NetBIOS域,以及在其中操作的容器
PrincipalContext ctx=新PrincipalContext(ContextType.Domain,“YOURDOMAIN”,“cn=通讯组,dc=YOURDOMAIN,dc=本地”);
//定义一个“原型”——一个你正在搜索的例子
//这里:只是一个简单的GroupPrincipal-您需要所有组
GroupPrincipal原型=新的GroupPrincipal(ctx);
//定义PrincipalSearcher以查找与原型匹配的主体
PrincipalSearcher=新PrincipalSearcher(原型);
//定义用于保存组名的字符串列表
列表组名=新列表();
//迭代.FindAll()调用的结果
foreach(searcher.FindAll()中的var gp)
{
//将结果强制转换为GroupPrincipal
GroupPrincipal group=gp作为GroupPrincipal;
//如果一切顺利-抓取组名并将其放入列表中
如果(组!=null)
{
groupNames.Add(group.Name);
}
}
这能满足你的需要吗


有关
System.DirectoryServices.AccountManagement
命名空间的更多信息,请阅读MSDN杂志上的文章。

以下是我提出的解决方案;对于感兴趣的人:

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;
}

谢谢Marc,我还没有试过你的代码,但无论如何,我会发布我为那些对这个主题感兴趣的人制作的解决方案。非常感谢。