简体   繁体   English

c#DirectoryEntry.Properties vs DirectoryEntry.InvokeGet?

[英]c# DirectoryEntry.Properties vs DirectoryEntry.InvokeGet?

I have a strange problem when I tried to retrieve the "AccountExpirationDate" from the active directory. 当我尝试从活动目录中检索“AccountExpirationDate”时,我遇到了一个奇怪的问题。

I use the following code to retrieve the user: 我使用以下代码来检索用户:

        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();

And then I retrieve the "AccountExpirationDate": 然后我检索“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

So I would like to what happened here? 所以我想发生在这里发生的事情? Why I cannot use DirectoryEntry.Properties to retrieve the AccountExpirationDate? 为什么我不能使用DirectoryEntry.Properties来检索AccountExpirationDate? What is the different between DirectoryEntry.Properties vs DirectoryEntry.InvokeGet? DirectoryEntry.Properties与DirectoryEntry.InvokeGet之间的区别是什么?

Thanks a lot. 非常感谢。

You can tell a directorySearcher which properties to load as follows: 您可以告诉directorySearcher要加载哪些属性,如下所示:

      // specify the search filter
      search.Filter = "(&(objectClass=user)(mail=" + email + "))";
      search.PropertiesToLoad.Add("AccountExpirationDate");
      search.PropertiesToLoad.Add("displayname");

after performing search you need to go through the properties of the SearchResult to get values ie 执行搜索后,您需要通过SearchResult的属性来获取值,即

      object o1 = result.Properties["AccountExpirationDate"][0];

DirectoryEntry.Properties - Gets the Active Directory Domain Services properties for this DirectoryEntry object. DirectoryEntry.Properties - 获取此DirectoryEntry对象的Active Directory域服务属性。 DirectoryEntry.InvokeGet - Gets a property from the native Active Directory Domain Services object. DirectoryEntry.InvokeGet - 从本机Active Directory域服务对象获取属性。

//Microsoft doesn't recommend the use of InvokeGet method. // Microsoft不建议使用InvokeGet方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM