要获取 Active Directory 中的所有组,我已经用 C# 编写了此代码。它工作得很好,因为我不需要传递任何服务器名、OU、DC 等。
UserPrincipal current_user = UserPrincipal.Current;
PrincipalContext current_context = current_user.Context;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal qbeUser = new GroupPrincipal(ctx);
Principal userOrGroup = qbeUser as Principal;
userOrGroup.Name = "*";
PrincipalSearcher searcher = new PrincipalSearcher(userOrGroup);
List<string> AllGroups = new List<string>();
// enumerate the results - you need to check what kind of principal you get back
foreach (Principal found in searcher.FindAll())
{
// is it a UserPrincipal - do what you need to do with that...
if (found is UserPrincipal)
{
// ......
}
else if (found is GroupPrincipal)
{
AllGroups.Add(found.Name);
//GroupPrincipal gp = found as GroupPrincipal;
//var data = gp.GetMembers();
// if it's a group - do whatever you need to do with a group....
}
}
//return AllGroups;
问题是它列出了太多我不需要的组
PerformanceLogUsers、SchemaAdmins、HelpServiceGroups、Telnet Clients 等等。
我只需要像管理员、来宾和其他用户创建的组这样的组。我读过一些类似的东西,比如特殊群体等等。
非常感谢这方面的任何帮助。