首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c# DirectoryEntry.Properties vs DirectoryEntry.InvokeGet?

c# DirectoryEntry.Properties vs DirectoryEntry.InvokeGet?
EN

Stack Overflow用户
提问于 2014-07-29 18:45:59
回答 1查看 1.7K关注 0票数 5

当我试图从活动目录中检索"AccountExpirationDate“时,我遇到了一个奇怪的问题。

我使用以下代码来检索用户:

代码语言:javascript
复制
        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":

代码语言:javascript
复制
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有什么不同?

非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2014-07-29 22:02:49

您可以告诉directorySearcher要加载哪些属性,如下所示:

代码语言:javascript
复制
      // specify the search filter
      search.Filter = "(&(objectClass=user)(mail=" + email + "))";
      search.PropertiesToLoad.Add("AccountExpirationDate");
      search.PropertiesToLoad.Add("displayname");

执行搜索后,您需要遍历SearchResult的属性以获得值,即

代码语言:javascript
复制
      object o1 = result.Properties["AccountExpirationDate"][0];

DirectoryEntry.Properties -获取此DirectoryEntry对象的Active Directory域服务属性。DirectoryEntry.InvokeGet -从本机Active Directory域服务对象获取属性。

//微软不推荐使用InvokeGet方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25013472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档