使用PrincipalSearcher获取广告组中的成员计数 [英] Get Count of members in a AD Group using PrincipalSearcher
问题描述
Env:Visual Studio 2013,FrameWork 4.5,Telerik控件,C#,WebForm应用程序
Env : Visual Studio 2013, FrameWork 4.5, Telerik Controls, C#, WebForm application
使用:System.DirectoryServices和System.DirectoryServices.AccountManagement
Using : System.DirectoryServices and System.DirectoryServices.AccountManagement
我正在使用搜索工具,以便用户可以在多个目录林/域中搜索活动目录组名称.
I'm making a search tools so a user can search for a active directory group name in multiple forest/domain.
搜索返回一个1个或多个组的列表,然后将该列表放入RadGrid(Telerik)中.网格的每一行都是一个AD组.我想显示一个附加信息,向用户显示该组中有多少(成员?)个成员.
The search return a list of 1 or more group and I put that list in a RadGrid (Telerik). Each row of the grid is a AD Group. I would like to display an additional information that show the user how many(count?) members(users) there is in that group.
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; }
}
感谢您的帮助
理查德
推荐答案
一种方法是使用 成员
集合属性或
One approach is to specify the type of the search result as GroupPrincipal
using .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()
method, which has an optional boolean argument to specify if you need to search the group recursively for nested members. At that point, get the size of the collection.
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();
这篇关于使用PrincipalSearcher获取广告组中的成员计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!