【问题标题】:Get Count of members in a AD Group using PrincipalSearcher使用 PrincipalSearcher 获取 AD 组中的成员计数
【发布时间】:2016-01-30 02:02:09
【问题描述】:

环境:Visual Studio 2013、FrameWork 4.5、Telerik Controls、C#、WebForm 应用程序

使用:System.DirectoryServices 和 System.DirectoryServices.AccountManagement

我正在制作一个搜索工具,以便用户可以在多个林/域中搜索活动目录组名称。

搜索返回一个包含 1 个或多个组的列表,我将该列表放入 RadGrid (Telerik)。网格的每一行都是一个 AD 组。我想显示一个附加信息,向用户显示该组中有多少(计数?)成员(用户)。

private List<AdGroup> GetListOfGroupAD(string domain, string name, string samAccountName)
    {
        try
        {
            GroupPrincipal qbeGroup;
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain))
            {
                qbeGroup = new GroupPrincipal(ctx);
                qbeGroup.Name = !string.IsNullOrEmpty(name) ? name : "*";
                qbeGroup.SamAccountName = !string.IsNullOrEmpty(samAccountName) ? samAccountName : "*";
                PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
                ((DirectorySearcher)srch.GetUnderlyingSearcher()).PageSize = 500;

                List<AdGroup> listeGroupe = srch.FindAll()
                                      .OrderBy(x => x.SamAccountName)
                                      .Select(x => new AdGroup()
                                      {
                                          SamAccountName = x.SamAccountName,
                                          Description = x.Description,
                                          Domain = domain,
                                          NbMember = 0 //Can i Get a count of members in group here ?????
                                      })
                                      .ToList();
                return listeGroupe;
            }
        }
        catch (ArgumentNullException ex)
        {
            writeToLog(ex.Message, 1);
            return null;
        }
        catch (Exception ex)
        {
            writeToLog(ex.Message, 1);
            return null;
        }
    }

public class AdGroup
    {
        public string SamAccountName { get; set; }
        public string Description { get; set; }
        public string Domain { get; set; }
        public int NbMember { get; set; }
    }

感谢您的帮助

理查德

【问题讨论】:

    标签: c# asp.net principalsearcher


    【解决方案1】:

    一种方法是在调用FindAll() 之后使用.OfType() 将搜索结果的类型指定为GroupPrincipal,然后您可以使用Members 集合属性将每个组的成员作为一个集合来获取或GetMembers() 方法,它有一个可选的布尔参数来指定是否需要递归地搜索组中的嵌套成员。此时,获取集合的大小。

    List<AdGroup> listeGroupe = srch.FindAll()
        .OfType<GroupPrincipal>()
        .OrderBy(x => x.SamAccountName)
        .Select(x => new AdGroup()
        {
            SamAccountName = x.SamAccountName,
            Description = x.Description,
            Domain = domain,
            NbMember = x.Members.Count
        })
        .ToList();
    

    【讨论】:

    • 谢谢您,我确实尝试了您的建议,但我收到此异常“尝试解析跨存储引用时,在主体 SID 指示的域中找不到目标主体。” :这是否表明该组的成员可能不是实际上下文中域的一部分?
    • 是的,该错误表明您域中某个组的成员的 SID 无效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多