【问题标题】:Get list of Roles, domain wise with Active Directory使用 Active Directory 获取角色列表,域明智
【发布时间】:2016-04-04 20:40:50
【问题描述】:

我试图在 C# 中列出来自 Active Directory 的角色列表,但到目前为止我发现的唯一答案是绑定到特定用户的角色列表,而不是域明智的所有角色列表。

这是我找到的sn-p here

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // find a user
   UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

   if(user != null)
   {
       // get the authorization groups - those are the "roles" 
       var groups = user.GetAuthorizationGroups();

       foreach(Principal principal in groups)
       {
           // do something with the group (or role) in question
       }
   }
}

如您所见,获取角色列表的唯一方法是使用绑定到用户的UserPrincipal 对象。我想列出可以分配给来自特定域的用户的所有可能角色。

顺便说一句,我不太了解 Active Directory 中的用户/组/角色层次结构,因此在考虑该结构的工作原理时我可能会出错,假设角色只是一种特殊的组。

【问题讨论】:

    标签: c# dns active-directory roles


    【解决方案1】:

    您可以使用PrincipalSearcher 和“示例查询”主体进行搜索:

    // create your domain context
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
       // define a "query-by-example" principal - here, we search for a GroupPrincipal 
       GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
    
       // create your principal searcher passing in the QBE principal    
       PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
    
       // find all matches
       foreach(var found in srch.FindAll())
       {
           // do whatever here - "found" is of type "Principal" - it could be user, group, computer....
       }
    }
    

    此搜索从当前连接域的顶级开始,并枚举该域中的所有组。现在,它们既可以是安全组,也可以是通讯组列表组 - 如果您想获得 安全组(真正对应于“角色”),您可以像这样调整您的搜索器:

     GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
     qbeGroup.IsSecurityGroup = true;
    

    有关更多详细信息,请参阅MSDN documentation on the System.DirectoryServices.AccountManagement 命名空间。

    【讨论】:

    • 谢谢,我已经对其进行了测试,它列出了我通过另一个 sn-p 获得的相同组。顺便说一句,如果我设置 qbeGroup.IsSecurityGroup = false 它返回一个空列表是否正常?不知道没有“正常组”是否正常,而且都是安全组。
    • @IvanMolinaHuerta:如果您设置了IsSecurityGroup=false,那么您返回您的 AD 中的 (Exchange) 通讯组。如果您没有在该服务器上配置 Exchange,您可能没有任何这些 - 这是完全可能的
    猜你喜欢
    • 2017-02-06
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多