如何使用.NET在LDAP存储库中检索类的完整属性列表

本文关键字:列表 属性 检索 NET 何使用 LDAP 存储 | 更新日期: 2023-09-27 18:26:53

我有一个从LDAP存储库读取用户记录的应用程序。我正在使用System.DirectoryServices.Protocols命名空间中的工具来完成此操作。对象类通常是person或inetOrgPerson。

如何从LDAP存储库中动态读取给定类的属性?

下面的代码为存储库中的示例用户生成一个属性列表,但似乎只返回那些有值的属性。

var connection = new LdapConnection(...);
SearchRequest request = new SearchRequest("CN=joe.user,DC=blah,DC=com", (string)null, SearchScope.Base);
SearchResponse response = (SearchResponse)connection.SendRequest(request);
var attributes = new List<string>();
foreach (SearchResultEntry entry in response.Entries)
{
    foreach (string attributeName in entry.Attributes.AttributeNames)
        attributes.Add(attributeName);
}

我可以从一个示例用户中获取objectclass属性来获取类,但是我如何检索用户的objectclass列表的所有属性呢?

注意:SearchRequest类声称将Attributes属性设置为null将返回所有属性。不幸的是,此属性没有setter!

注2:我尝试将"*"answers"+"添加到属性名称列表中,但没有成功。

如何使用.NET在LDAP存储库中检索类的完整属性列表

要读取目录项中填充的属性,请使用以下语法@objectClassName,例如@inetOrgPerson。请求此构造作为请求之一属性。另请参阅LDAP:检索对象类。此语法为在RFC 4529中定义。

要定位模式,请从根中提取属性subschemaSubEntry的值DSE。此属性的值是架构的根。可能是错误配置服务器可以阻止客户端读取subschemaSubEntry属性的值,但是对于管理员来说,这将是一个严重的错误,因为所有LDAP客户端都必须发现匹配的规则和比较属性值时使用的排序。

有关根DSE的更多信息;LDAP:根DSE";。

如果您使用的是DirectorySearcher/DirectoryEntry,则可以获得具有SchemaEntry属性的DirectoryEntry的架构对象。然后,您可以从模式条目中获得allowedAttributes构造的属性。

using System.DirectoryServices;
DirectoryEntry deTargetUser = new DirectoryEntry("LDAP://CN=joe.user,DC=blah,DC=com");
DirectorySearcher dsSchema = new DirectorySearcher(deTargetUser.SchemaEntry);
dsSchema.SearchScope = SearchScope.Base; //allowedAttributes is a constructed attribute, so have to ask for it while performing a search
dsSchema.Filter = "(objectClass=*)"; //this is closest thing I can find to an always true filter
dsSchema.PropertiesToLoad.Add("allowedAttributes");
SearchResult srSchema = dsSchema.FindOne();
var attributes = new List<string>();
foreach(string attributeName in srSchema.Properties["allowedAttributes"])
{
    attributes.Add(attributeName);
}

如果您只想要属性名称,可以通过objectClass的模式获取它们。