如果您使用的是 .NET 3.5 及更高版本,则应查看System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关信息:
基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "MainGroup");
// if found....
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
// do whatever you need to do to those members
// if you need to find the groups that are members of 'MainGroup'
GroupPrincipal group = p as GroupPrincipal;
if(group != null)
{
// now you have a group that is member of 'MainGroup' - do what you need here
}
}
}
}
新的 S.DS.AM 使得在 AD 中与用户和组一起玩变得非常容易!