【问题标题】:How to find a list of users in a specific department using LDAP如何使用 LDAP 查找特定部门的用户列表
【发布时间】:2014-01-20 05:24:13
【问题描述】:

如何使用 DirectorySearcher 和 Filter/PropertiesToLoad 获取特定部门中所有用户的列表?

我知道如何使用用户名进行过滤并获取用户的部门名称,但我不知道如何指定部门并获取属于该部门的员工列表。

感谢任何帮助!

例如

var search = new DirectorySearcher(new DirectoryEntry("LDAP://DC=au,DC=company,DC=com"));
search.Filter = "(sAMAccountName=" + userID + ")"; // put the identity name here
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("department");
var res = search.FindOne();

【问题讨论】:

    标签: asp.net active-directory ldap directoryservices directorysearcher


    【解决方案1】:

    如果你想使用旧式的DirectorySearcher,那么技巧就是绑定到你想列出用户的OU,例如。你的部门:

    var searchRoot = new DirectoryEntry("LDAP://OU=YourDepartment,DC=au,DC=company,DC=com");
    var search = new DirectorySearcher(searchRoot);
    

    然后做一个

    search.FindAll();
    

    并迭代结果。

    另一种选择是使用较新的 System.DirectoryServices.AccountManagement 命名空间并使用它的强类型、易于使用的类,例如 PrincipalSearcher 和“按示例查询”主体来进行搜索:

    // create your domain context and define a "starting" container where to search in
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=YourDepartment,DC=au,DC=company,DC=com"))
    {
       // define a "query-by-example" principal - here, we search for a UserPrincipal 
       // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
       UserPrincipal qbeUser = new UserPrincipal(ctx);
       qbeUser.GivenName = "Bruce";
       qbeUser.Surname = "Miller";
    
       // create your principal searcher passing in the QBE principal    
       PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
    
       // find all matches
       foreach(var found in srch.FindAll())
       {
           // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
       }
    }
    

    如果您还没有 - 一定要阅读 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能。或查看MSDN documentation on the System.DirectoryServices.AccountManagement 命名空间。

    当然,根据您的需要,您可能希望在您创建的“示例查询”用户主体上指定其他属性:

    • DisplayName(通常:名字 + 空格 + 姓氏)
    • SAM Account Name - 您的 Windows/AD 帐户名
    • User Principal Name - 您的“username@yourcompany.com”样式名称

    您可以在UserPrincipal 上指定任何属性并将其用作PrincipalSearcher 的“示例查询”。

    更新:要获取组的成员,请使用以下代码:

    // set up domain context
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
        // find the group in question
        GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
    
        // if found....
        if (group != null)
        {
           // iterate over members
           foreach (Principal p in group.GetMembers())
           {
               Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
               // do whatever you need to do to those members
           }
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我实际上不是在寻找部门内的员工,而是在特定 AD 组(我们组成的组)内的员工列表。在这种情况下,我还会使用相同的解决方案吗?它似乎对我不起作用..
    • @viv_acious:不,组与 OU 不同。一个组有成员 - 它不包含用户。您需要找到该组,然后获取其成员。我会更新我的帖子....
    • 非常感谢 marc_s。我现在试一试,然后告诉你。非常感谢您的帮助!
    • 嗨@mar​​c_s,只是想问你一些与此相关的问题 - 如果一个组有成员和嵌套的子组(并且每个子组都有自己的成员和嵌套的子组),我将如何更改解决方案?非常感谢您在这方面的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多