Asp.net 如何使用LDAP查找特定部门中的用户列表

Asp.net 如何使用LDAP查找特定部门中的用户列表,asp.net,active-directory,ldap,directoryservices,directorysearcher,Asp.net,Active Directory,Ldap,Directoryservices,Directorysearcher,如何使用DirectorySearcher和Filter/PropertiesToLoad获取特定部门中所有用户的列表 我知道如何使用用户名进行筛选并获取用户的部门名称,但我不知道如何指定部门并获取属于该部门的员工列表 感谢您的帮助 e、 g 如果您想使用老式的DirectorySearcher,那么诀窍是绑定到要列出用户的OU,例如您的部门: var searchRoot = new DirectoryEntry("LDAP://OU=YourDepartment,DC=au,DC=compa

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

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

感谢您的帮助

e、 g


如果您想使用老式的
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文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement
中的新功能。或者查看名称空间

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

  • DisplayName
    (通常为:名字+空格+姓氏)
  • SAM帐户名
    -您的Windows/AD帐户名
  • 用户主体名称
    -您的”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
       }
    }
}

谢谢你的帮助。我不是在寻找一个部门内的员工,而是在寻找一个特定广告组(我们组成的组)内的员工名单。在这种情况下,我还会使用相同的解决方案吗?这对我来说似乎不起作用。@viv_êicious:不,团体不同于OU。组有成员-不包含用户。您需要找到该组,然后获取其成员。我会更新我的帖子…非常感谢marc_。我现在就试试,让你知道。非常感谢你的帮助!您好@marc_s,我想问您一些与此相关的问题-如果一个组有成员和嵌套子组(每个子组都有自己的成员和嵌套子组),我将如何更改解决方案?非常感谢你在这方面的帮助!
// 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
       }
    }
}