使用 PrincipalSearcher 获取 AD 组中的成员数
Get Count of members in a AD Group using PrincipalSearcher
环境:Visual Studio 2013,FrameWork 4.5,Telerik 控件,C#,WebForm 应用程序
使用:System.DirectoryServices 和 System.DirectoryServices.AccountManagement
我正在制作一个搜索工具,以便用户可以在多个 forest/domain 中搜索活动目录组名称。
搜索 return 1 个或多个组的列表,我将该列表放在 RadGrid (Telerik) 中。网格的每一行都是一个广告组。我想显示一个附加信息,向用户显示该组中有多少(计数?)成员(用户)。
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; }
}
感谢您的帮助
理查德
一种方法是使用 .OfType()
after the call to FindAll()
, and then you can get the members of each group as a collection using the Members
collection property or the GetMembers()
方法将搜索结果的类型指定为 GroupPrincipal
,它有一个可选的布尔参数来指定是否需要递归搜索组以查找嵌套成员。到那时,获取集合的大小。
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();
环境:Visual Studio 2013,FrameWork 4.5,Telerik 控件,C#,WebForm 应用程序
使用:System.DirectoryServices 和 System.DirectoryServices.AccountManagement
我正在制作一个搜索工具,以便用户可以在多个 forest/domain 中搜索活动目录组名称。
搜索 return 1 个或多个组的列表,我将该列表放在 RadGrid (Telerik) 中。网格的每一行都是一个广告组。我想显示一个附加信息,向用户显示该组中有多少(计数?)成员(用户)。
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; }
}
感谢您的帮助
理查德
一种方法是使用 .OfType()
after the call to FindAll()
, and then you can get the members of each group as a collection using the Members
collection property or the GetMembers()
方法将搜索结果的类型指定为 GroupPrincipal
,它有一个可选的布尔参数来指定是否需要递归搜索组以查找嵌套成员。到那时,获取集合的大小。
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();