【问题标题】:How to get all the properties from a DirectoryEntry object如何从 DirectoryEntry 对象中获取所有属性
【发布时间】:2016-10-08 08:22:17
【问题描述】:

我尝试编写一个程序来获取可用DirectoryEntry 中的所有属性

// create LDAP connection object  

DirectoryEntry myLdapConnection = createDirectoryEntry();  

// create search object which operates on LDAP connection object  
// and set search object to only find the user specified  

DirectorySearcher search = new DirectorySearcher(myLdapConnection);  
search.Filter = "(cn=" + username + ")";  

// create results objects from search object  

SearchResult result = search.FindOne();  

if (result != null)  
{  
    // user exists, cycle through LDAP fields (cn, telephonenumber etc.)  

    ResultPropertyCollection fields = result.Properties;  

    foreach (String ldapField in fields.PropertyNames)  
    {  
         // cycle through objects in each field e.g. group membership  
         // (for many fields there will only be one object such as name)  

         foreach (Object myCollection in fields[ldapField])   
             Console.WriteLine(String.Format("{0,-20} : {1}",   
                    ldapField, myCollection.ToString()));  
    }  
 }  

 else  
 {  
      // user does not exist  
      Console.WriteLine("User not found!");  
 }

当我尝试在一个用户下执行此过程时,它会返回一个属性列表,但是当我尝试在另一个用户下执行此过程时,它会返回不同数量的属性。

我需要什么样的授权才能获得所有的属性?

提前致谢

科比

【问题讨论】:

    标签: .net active-directory ldap


    【解决方案1】:

    默认情况下,所有成功登录的 AD 帐户(经过身份验证的用户)都具有对 Active Directory 的读取权限(请参阅https://blog.varonis.com/the-difference-between-everyone-and-authenticated-users/)。但是在某些情况下,管理员可能会更改行为。要检查用户可以访问哪些 AD 对象属性,请在任何 DC 上打开 ADUC 管理单元,打开所需的 AD 对象属性,转到安全选项卡。在那里,您可以看到所有用户\组的通用安全信息。如果您单击选项卡上的高级按钮,然后转到有效访问选项卡,您可以验证所需的安全主体对对象的有效访问权限。

    最简单的方法是使用属于 Domain Admins\Enterprise Admins 组的帐户连接到 DC。但是,出于安全原因,我建议避免这种情况并正确配置权限

    【讨论】:

      【解决方案2】:

      使用 LDAP:

      DirectoryEntry entry = new DirectoryEntry("LDAP://KLM.ENS");
      
      using (DirectorySearcher adSearch = new DirectorySearcher(entry))
      {
          adSearch.Filter = "(sAMAccountName=userName)";
          SearchResult adSearchResult = adSearch.FindOne();
          foreach(var propertyName in adSearchResult.Properties.PropertyNames)
          {
              Console.WriteLine($"{propertyName} : {adSearchResult.Properties[propertyName.ToString()][0]}");                        
          }
      }
      

      使用 WinNT:

      string Domain_Slash_Machine = System.Web.HttpContext.Current.User.Identity.Name;
      Domain_Slash_Machine = Domain_Slash_Machine.Replace(@"\", @"/");
      string queryString = @"WinNT://" + Domain_Slash_Machine;
      
      DirectoryEntry obDirEntry = new DirectoryEntry(queryString);
      System.DirectoryServices.PropertyCollection propColl =
      obDirEntry.Properties;
      foreach (var propertyName in obDirEntry.Properties.PropertyNames)
      {
          Console.WriteLine($"{propertyName} : {propColl[propertyName.ToString()].Value}");                    
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 2015-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-07
        • 2021-09-10
        相关资源
        最近更新 更多