Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 474
KenLai Avatar asked Nov 11 '22 04:11

KenLai


1 Answers

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 i.e.

      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.

like image 50
Bayeni Avatar answered Nov 15 '22 12:11

Bayeni