2011-12-14 117 views
3

假设用户johnsmith是活动目录组MyManagers的成员。 假设组MyManagers是MyEmployees组的成员。 假设组MyEmployees是MyUsers组的成员。如何检查Active Directory组是否是另一个Active Directory组的成员

当johnsmith登录到我的应用程序时,我如何知道他是MyUsers组的成员?

欣赏C#中的示例。

感谢, kruvi

+0

你试过了什么? – 2011-12-14 14:41:42

回答

4

如果你在.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 
} 

GetAuthorizationGroups S.DS.AM中的调用确实进行了递归查询,例如它也会选择你的用户所属的任何群组,因为群组是其他群组的成员。

新的S.DS.AM可以很容易地与AD中的用户和群组玩耍!

相关问题