【问题标题】:How I get the users of a folder in Active Directory?如何获取 Active Directory 中文件夹的用户?
【发布时间】:2013-10-08 07:48:22
【问题描述】:

您好,我想构建一个 LDAP 查询,如何在一个文件夹中获取所有用户...例如:

OU=DBG,OU=THINCLIENT,OU=NPS,OU=services,DC=YourDomain,DC=com

我想从 Active Directory 中获取此文件夹中的所有用户。为此,我有一个查询,但我不知道如何获取此文件夹的用户:(

(&(objectClass=user)(objectCategory=user)(??????))

【问题讨论】:

    标签: c# asp.net filter active-directory ldap


    【解决方案1】:

    如果您使用的是 .NET 3.5 或更高版本,则可以使用 PrincipalSearcher 和“query-by-example”主体进行搜索:

    // create your domain context
    string container = "OU=DBG,OU=THINCLIENT,OU=NPS,OU=services,DC=YourDomain,DC=com";
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YourDomain", container))
    {
       // define a "query-by-example" principal - here, we search for UserPrincipal 
       UserPrincipal qbeUser = new UserPrincipal(ctx);
    
       // 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 命名空间。

    您需要在引用中添加对System.DirectoryServices.AccountManagement 程序集的引用,并且需要这样一行:

    using System.DirectoryServices.AccountManagement;
    

    在您的代码隐藏文件的顶部,以使其正常工作。

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

    【讨论】:

      【解决方案2】:

      您可以使用 System.DirectoryServices 命名空间。

       DirectoryEntry scope = new   DirectoryEntry("LDAP://OU=DBG,OU=THINCLIENT,OU=NPS,OU=services,DC=YourDomain,DC=com");
      
       string filter = "(&(objectClass=user)(objectCategory=user))";
       string[] attrs = new string[]{"samaccountname","whencreated"};
       DirectorySearcher searcher = new  DirectorySearcher(scope,filter,attrs);
      
       foreach(SearchResult result in searcher.FindAll())
       {
           //result.Properties["attribute"][0].ToString();
       }
      

      【讨论】:

        【解决方案3】:

        试试

        (&(objectClass=user)(objectCategory=user)(homeDirectory=*YourFolderName*))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-26
          • 1970-01-01
          相关资源
          最近更新 更多