C# 如何检查Active Directory组是否是另一个Active Directory组的成员
假设用户johnsmith是活动目录组MyManagers的成员。 假设MyManagers组是MyEmployees组的成员。 假设MyEmployees组是MyUsers组的成员 当johnsmith登录到我的应用程序时,我怎么知道他是MyUsers组的成员 欣赏C#中的例子 谢谢,C# 如何检查Active Directory组是否是另一个Active Directory组的成员,c#,.net,active-directory,active-directory-group,C#,.net,Active Directory,Active Directory Group,假设用户johnsmith是活动目录组MyManagers的成员。 假设MyManagers组是MyEmployees组的成员。 假设MyEmployees组是MyUsers组的成员 当johnsmith登录到我的应用程序时,我怎么知道他是MyUsers组的成员 欣赏C#中的例子 谢谢, kruvi如果您使用的是.NET 3.5及更高版本,则应检查System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。请在此处阅读所有相关内容: 基
kruvi如果您使用的是.NET 3.5及更高版本,则应检查
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:
// 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
}
//设置域上下文
PrincipalContext ctx=新PrincipalContext(ContextType.Domain);
//查找用户
UserPrincipal user=UserPrincipal.Current;//这将是约翰·史密斯
如果(用户!=null)
{
//获取用户所属的组
PrincipalSearchResult results=user.GetAuthorizationGroups();
//现在,您只需要遍历这些组,看看是否找到
//一个你感兴趣的群体
}
S.DS.AM中的GetAuthorizationGroups
调用确实会进行递归查询,例如,由于组是其他组的成员,因此它还会提取用户所属的任何组
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易