c# DirectoryEntry.Properties与DirectoryEntry.InvokeGet的区别是什么?

5

当我尝试从活动目录中检索“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有何不同?
非常感谢。
1个回答

2
您可以按照以下方式告诉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 Domain Services属性。 DirectoryEntry.InvokeGet - 从本机Active Directory Domain Services对象获取属性。

//Microsoft不建议使用InvokeGet方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接