假设用户 johnsmith 是活动目录组 MyManagers 的成员。假设组 MyManagers 是组 MyEmployees 的成员。假设组 MyEmployees 是组 MyUsers 的成员。
当 johnsmith 登录到我的应用程序时,我怎么知道他是 MyUsers 组的成员?
欣赏 C# 中的示例。
谢谢,克鲁维
假设用户 johnsmith 是活动目录组 MyManagers 的成员。假设组 MyManagers 是组 MyEmployees 的成员。假设组 MyEmployees 是组 MyUsers 的成员。
当 johnsmith 登录到我的应用程序时,我怎么知道他是 MyUsers 组的成员?
欣赏 C# 中的示例。
谢谢,克鲁维
如果您使用的是 .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 中与用户和组一起玩变得非常容易!