【发布时间】:2014-02-26 17:52:22
【问题描述】:
我正在使用 c#、DirectoryEntry (System.DirectoryServices) 来读取和写入 objectClass "inetOrgPerson" 的用户。 这很好用,直到我想写属性“labeledURI”或“displayName”。 实际上,我可以写一次,但第二次失败了。我认为 DirectoryEntry 很难从 Ldap 中读取它们。 我可以提供更多代码,但我希望这个小例子能提供一个想法。
这里是伪代码:
DirectoryEntry deFound = null;
DirectoryEntry de = new DirectoryEntry(a_strLdapServer + "/" + a_strLdapBasePath, m_strLdapUser, m_strLdapPw, AuthenticationTypes.None);
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=person) (uid=" + a_strUserName + "))";
//deSearch.PropertiesToLoad.Add("labeledURI");
SearchResultCollection results = deSearch.FindAll();
if (results.Count == 1)
{
deFound = results[0].GetDirectoryEntry();
}
//WORKS ONCE:
deFound.Properties["labeledURI"].Value = "http://www.google.com";
抛出的异常: deFound.Properties["labeledURI"].Value 'm_deFound.Properties["labeledURI"]' 引发了类型为 'System.Runtime.InteropServices.COMException' 对象的异常
你知道我做错了吗?
【问题讨论】:
-
发现从 SearchResult 中读取“labeledURI”值:Byte[] bytes = (Byte[]) results[0].Properties["labeledURI"][0]; string strValue = System.Text.Encoding.UTF8.GetString(bytes);