我在我的 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/