【问题标题】:Active Directory : Get all "Security Group"Active Directory:获取所有“安全组”
【发布时间】:2011-09-06 08:47:30
【问题描述】:

我想在 Active Diectory 中获取所有可用的组“安全组”。

有什么想法吗?

谢谢,

【问题讨论】:

    标签: .net c#-4.0 active-directory


    【解决方案1】:

    由于您使用的是 .NET 3.5 或更高版本,您可以使用 PrincipalSearcher 和“按示例查询”主体进行搜索:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a GroupPrincipal 
    // with the security group flag set
    GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
    qbeGroup.IsSecurityGroup = true;
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
    
    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    }
    

    如果您还没有 - 一定要阅读 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能

    【讨论】:

    • 当我发现.GetGroups() 异常“服务器无法运行”但在我获得与用户关联的组之前。事实上,我想得到所有。我的解释不好,我要OU(Organisation Unit)“安全组”的组
    【解决方案2】:

    试试这个方法

               DirectoryEntry ent1 = new DirectoryEntry("LDAP://" + _path, 
               "adminUser", "***********");
    
                DirectorySearcher dSearch = new DirectorySearcher(ent1);
    
                dSearch.Filter = "(&(objectClass=group))";
    
                dSearch.SearchScope = SearchScope.Subtree;
    
                SearchResultCollection results = dSearch.FindAll();
                List<string> groupNames = new List<string>();
    
                for (int i = 0; i < results.Count; i++)
                {
                    DirectoryEntry de = results[i].GetDirectoryEntry();
    
                    groupNames.Add(de.Name.Replace("CN=", ""));
    
    
                }
    

    它对我有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多