简体   繁体   中英

c# DirectoryEntry.Properties vs DirectoryEntry.InvokeGet?

I have a strange problem when I tried to retrieve the "AccountExpirationDate" from the active directory.

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":

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? What is the different between DirectoryEntry.Properties vs DirectoryEntry.InvokeGet?

Thanks a lot.

You can tell a directorySearcher which properties to load as follows:

      // 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

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

DirectoryEntry.Properties - Gets the Active Directory Domain Services properties for this DirectoryEntry object. DirectoryEntry.InvokeGet - Gets a property from the native Active Directory Domain Services object.

//Microsoft doesn't recommend the use of InvokeGet method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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