【问题标题】:How to check if an Active Directory group is a member of a another Active Directory group如何检查 Active Directory 组是否是另一个 Active Directory 组的成员
【发布时间】:2011-12-14 14:40:44
【问题描述】:
假设用户 johnsmith 是活动目录组 MyManagers 的成员。
假设组 MyManagers 是组 MyEmployees 的成员。
假设组 MyEmployees 是组 MyUsers 的成员。
当 johnsmith 登录到我的应用程序时,我怎么知道他是 MyUsers 组的成员?
欣赏 C# 中的示例。
谢谢,
克鲁维
【问题讨论】:
标签:
c#
.net
active-directory
active-directory-group
【解决方案1】:
如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:
基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.Current; // this would be John Smith
if(user != null)
{
// get the user's groups he's a member of
PrincipalSearchResult<Principal> results = user.GetAuthorizationGroups();
// now you just need to iterate over the groups and see if you find the
// one group you're interested in
}
S.DS.AM 中的GetAuthorizationGroups 调用确实会进行递归查询,例如它还会选择您的用户所属的任何组,因为组是其他组的成员。
新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!