Search code examples
active-directoryldap

Why am I getting 'System.__ComObject' from my LDAP property?


I want to display the account expiry date—either as something readable, or in ticks—it doesn't matter:

DirectorySearcher searcher = new DirectorySearcher();
searcher.Filter = String.Format( "(SAMAccountName={0})", "TestA33" );
searcher.PropertiesToLoad.Add( "cn" );

SearchResult result = searcher.FindOne();
DirectoryEntry uEntry = result.GetDirectoryEntry();

String expiry = uEntry.Properties["accountExpires"].Value.ToString(); 

Response.Write( expiry );

What I get instead is: System.__ComObject


Solution

  • It's because the property value is represented using the ADSI IADsLargeInteger COM interface and needs to be converted to a .NET date. Although I haven't tried it, there is a sample that shows how here: http://www.simple-talk.com/dotnet/.net-framework/building-active-directory-wrappers-in-.net/