2010-06-07 58 views
0

我有以下代码。我得到一个用户的目录条目(strpath)。 然后我得到用户列出的组。在活动目录中查找组大小

我怎样才能获得每组用户的数量?

DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(strpath); 
object obGroups = myDE.Invoke("Groups"); 
foreach (object ob in (IEnumerable)obGroups) 
{ 
    DirectoryEntry obGpEntry = new DirectoryEntry(ob); 
    GroupsListBox.Items.Add(obGpEntry.Name); 
} 

回答

1

如果你在.NET 3.5(或可以升级到它),有一个大规模扩展System.DirectoryServices.AccountManagement命名空间,使得管理用户,组和他们的成员轻松许多的这些作业。

查看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5了解S.DS.AM的介绍。

你可以得到一个用户主要是这样的:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"); 

UserPrincipal user = UserPrincipal.FindByIdentity("some user name"); 

PrincipalSearchResult<Principal> userGroups = user.GetGroups(); 

foreach (Principal p in myGroups) 
{ 
    GroupPrincipal gp = (p as GroupPrincipal); 

    if (gp != null) 
    { 
     int memberCount = gp.Members.Count; 
    } 
} 

这种方式,您可以枚举所有组给定的用户,并列举出这些群体,你可以找出有多少成员(用户和其他组)每个组都有。