![](/img/trans.png)
[英]Get ONLY the count of members of 'GROUP' type from an AAD group using Graph API
[英]Get Count of members in a AD Group using PrincipalSearcher
Env:Visual Studio 2013,FrameWork 4.5,Telerik控件,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; }
}
感謝您的幫助
理查德
一種方法是在調用FindAll()
.OfType()
之后,使用.OfType()
將搜索結果的類型指定為GroupPrincipal
,然后可以使用Members
collection屬性或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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.