c# - 在 C# 中通过 System.DirectoryServices 查询自定义 LDAP 属性?

标签 c# asp.net .net ldap openldap

我在我的 OpenLDAP 服务器上安装了一个自定义 LDAP 架构,如下所示:

attributeType ( 999.0.01
    NAME 'picturePath'
    EQUALITY caseIgnoreMatch
    SUBSTR caseIgnoreSubstringsMatch
    SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{1024}
    )

objectClass ( 999.1.01
    NAME 'indieStackTeam'
            DESC 'Team definition for IndieStack'
    SUP groupOfUniqueNames
    STRUCTURAL
            MAY     ( picturePath )
    )

在我的 ASP.NET MVC 2 应用程序中,我正在查询 picturePath 属性(并确认 picturePath 存在于键列表中):

this.Picture = properties["picturePath"].Value as string;

当我尝试在 .NET 3.5 下执行此操作时,出现以下异常:

[COMException (0x8000500c): Unknown error (0x8000500c)]   
    System.DirectoryServices.PropertyValueCollection.PopulateList() +347013
    System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +49   
    System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +150

但是,当相同的代码在 Mono 下运行时(与 OpenLDAP 在同一台服务器上),它工作得非常好。 LDAPAdmin 等客户端也可以正确读取 picturePath 属性。

更重要的是,只有当我去读取它失败的值时;我可以在 key 列表中看到该属性,但我无法访问它。

不幸的是,未知错误并没有告诉我很多关于出了什么问题,但我发现 System.DirectoryServices 的 .NET 实现非常不稳定(如果您使用小写字母连接到 LDAP 服务器,您会得到同样的未知错误在“DC=”中)。

有没有人遇到过这个问题,如果有,怎么解决的?

最佳答案

你应该检查两件事:

1) 该特定用户对象在 picturePath 中确实有值吗?您可能希望在访问该属性之前检查该属性是否存在:

if(properties.Contains("picturePath") && properties["picturePath"].Count > 0)
{
   ....
}

2) 如果我没记错的话,要访问自定义属性,您应该在执行任何操作之前显式刷新用户对象的缓存:

DirectoryEntry de = ......;  // find / assign that DirectoryEntry somehow

de.RefreshCache();  // to load all properties from the directory

或:

de.RefreshCache(new string[] { "picturePath" });  // to just load the "picturePath" attribute

此外:System.DirectoryServices 中的类实际上主要用于针对 Active Directory - 当用于某些其他 LDAP 服务器(如 OpenLDAP)时可能会出现“意外”或细微的不兼容性。

关于c# - 在 C# 中通过 System.DirectoryServices 查询自定义 LDAP 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549060/

相关文章:

c# - ASP.NET 用户控件 : can't initialize a user control property using Eval ("...")

c# - 您在 C# 的 Program.cs 中放入了哪些常用例程

c# - 不确定如何修复这个奇怪的 Entity Framework (关系/导航)错误

c# - EntityFramework 中使用 'where' 语句的通用查询

c# - System.ArgumentOutOfRangeException Message=长度不能小于零。 (参数 'length')

javascript - 为什么这是未定义的以及如何解决这个问题? C#

c# - Google Adword 和 asp.net 母版页

c# - 如何在 C# 代码中安全地保存连接字符串?

c# - 与 IsNullable 冲突的配置设置 = true IsNullable = false 重用 ComplexType

.net - 在 ComboBox 中以不同方式显示所选项目