Reading/Filtering Distribution Group's Subgroups of an active directory?

让人想犯罪 __ 提交于 2019-12-22 15:37:30

问题


I've an Active Directory with domain myDomain.local, under it there exists a Distribution Group that contains many groups.
How can I read (programmatically) all these subgroups to retrieve a list of their names ?
And how to optimize the query to filter the result so that it just retrieves all the groups that ends with the word Region ?
BTW, I'm using C#.Net, ASP.Net and sharepoint, and i'm not experienced with AD.


回答1:


If you're on .NET 3.5 (or can upgrade to it), you can use this code using the System.DirectoryServices.AccountManagement namespace:

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

Does that satisfy your needs?

For more info on the System.DirectoryServices.AccountManagement namespace, read the Managing Directory Security Principals in the .NET Framework 3.5 article in MSDN magazine.




回答2:


Here's the solution I made; for those who are interested:

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


来源:https://stackoverflow.com/questions/3159424/reading-filtering-distribution-groups-subgroups-of-an-active-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!