【问题标题】:Reading/Filtering Distribution Group's Subgroups of an active directory?读取/过滤 Active Directory 的通讯组的子组?
【发布时间】:2010-07-01 15:47:54
【问题描述】:

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

【问题讨论】:

    标签: c# sharepoint active-directory distribution active-directory-group


    【解决方案1】:

    如果您使用的是 .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 文章。

    【讨论】:

    • 谢谢 Marc,我还没有尝试你的代码,但无论如何我会发布我为那些对该主题感兴趣的人制作的解决方案。非常感谢。
    【解决方案2】:

    这是我提出的解决方案;有兴趣的朋友:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2011-06-21
      • 2017-11-26
      • 2011-11-05
      • 2011-07-14
      相关资源
      最近更新 更多