当我试图从活动目录中检索"AccountExpirationDate“时,我遇到了一个奇怪的问题。
我使用以下代码来检索用户:
DirectoryEntry dirEntry = new DirectoryEntry(Path);
DirectorySearcher search = new DirectorySearcher(dirEntry);
// specify the search filter
search.Filter = "(&(objectClass=user)(mail=" + email + "))";
// perform the search
SearchResult result = search.FindOne();
DirectoryEntry user = result.GetDirectoryEntry();然后我检索"AccountExpirationDate":
object o1 = user.Properties["accountExpires"].Value; //return a COM object and I cannot retrieve anything from it
object o2 = user.Properties["AccountExpirationDate"].Value; //return null
object o3 = user.InvokeGet("AccountExpirationDate"); //return the DateTime所以我想知道这里发生了什么?为什么不能使用DirectoryEntry.Properties检索AccountExpirationDate?DirectoryEntry.Properties和DirectoryEntry.InvokeGet有什么不同?
非常感谢。
发布于 2014-07-29 22:02:49
您可以告诉directorySearcher要加载哪些属性,如下所示:
// specify the search filter
search.Filter = "(&(objectClass=user)(mail=" + email + "))";
search.PropertiesToLoad.Add("AccountExpirationDate");
search.PropertiesToLoad.Add("displayname");执行搜索后,您需要遍历SearchResult的属性以获得值,即
object o1 = result.Properties["AccountExpirationDate"][0];DirectoryEntry.Properties -获取此DirectoryEntry对象的Active Directory域服务属性。DirectoryEntry.InvokeGet -从本机Active Directory域服务对象获取属性。
//微软不推荐使用InvokeGet方法。
https://stackoverflow.com/questions/25013472
复制相似问题