Chris Cudmore
Chris Cudmore

Reputation: 30161

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

Upvotes: 5

Views: 13492

Answers (4)

stackprotector
stackprotector

Reputation: 13578

Do not use GetDirectoryEntry(), access the properties directly instead, e. g.:

#...
SearchResult result = searcher.FindOne();

String expiry = result.Properties["accountExpires"][0].ToString(); 

Response.Write( expiry );

The returned number is a Windows timestamp, similar but not the same as a Unix timestamp. You can convert it to a DateTime object via DateTime.FromFileTimeUtc.


PowerShell equivalent:

[Datetime]::FromFileTimeUtc(([adsisearcher] "(sAMAccountName=TestA33)").FindOne().Properties.accountexpires[0])

Upvotes: 0

zebra
zebra

Reputation: 1338

with this you can get the real value

Microsoft.VisualBasic.Information.TypeName(item);

Upvotes: -2

barneytron
barneytron

Reputation: 7963

The article that Dave Cluderay recommended is a good idea. One important thing to note is that if expiration is set to never, the date you get might not make sense.

According to MS documentation, the IADsLargeInteger from the ADSI call represents the number of 100 nanosecond intervals since Jan 1, 1601 (UTC) and "value of 0 or 0x7FFFFFFFFFFFFFFF (9223372036854775807) indicates that the account never expires".

Upvotes: 6

Dave Cluderay
Dave Cluderay

Reputation: 7426

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/

Upvotes: 5

Related Questions