Skip to main content
 首页 » 编程设计

c#之AD 中用户的 LDAP 查询带回计算机对象

2024年02月13日12findumars

我在 AD 中使用以下查询来取回用户的邮件地址:

// get a DirectorySearcher object 
         DirectorySearcher search = new DirectorySearcher(); 
 
         // specify the search filter 
         search.Filter = "(&(objectClass=user)(anr=" + login + "))"; 
 
         // specify which property values to return in the search 
         search.PropertiesToLoad.Add("mail");        // smtp mail address 
 
         // perform the search 
         SearchResult result = search.FindOne(); 
 
         if (result != null) 
         { 
 
             return result.Properties["mail"][0].ToString(); 
         } 
         else 
         { 
 
             return null; 
         } 

对于登录名是“SRB”的特定用户,查询将返回一个名为“SRB-PC”的计算机对象。

我不明白为什么过滤器说 objectClass 应该是“用户”。

另外 - 为什么它把它带回来,因为它是一个“喜欢”的查询 - 我希望它只带回名称与过滤器完全匹配的对象。

请您参考如下方法:

A Computer object 是 User 的子 Class Active Directory 中的对象。这就是您为最初的搜索找到计算机的原因。

objectCategory 元素用于正确区分实体:-

如 Object Class vs. Object Category 中所述页:

Prior to Windows Server 2008, the objectClass attribute is not indexed. This is because it has multiple values and is highly non-unique; that is, every instance of the objectClass attribute includes the top class. This means an index would be very large and ineffective. To locate objects of a given class, use the objectCategory attribute, which is single-valued and indexed. For more information about using these properties in search filters, see Deciding What to Find.

因此使用 objectCategory 而不是 objectClass 进行搜索效率要高得多。