views:

55

answers:

1

I'd like to retrieve the creation date of users in Active Directory. I understand that users have a WhenCreated property, but I cannot see that property exposed on the UserPrincipal type I am working with.

I'd like to do something like this:

var principal = UserPrincipal.FindByIdentity(context, IdentityType.Guid, guid);
//var createdDate = principal.CreationDate; 

Edit: this is a web application running within IIS, querying an AD server on another machine on the same network.

+3  A: 

You need to get the underlying DirectoryEntry (part of the System.DirectoryServices-namespace).

You can use this extension method to get the date as a string:

public static String GetProperty(this Principal principal, String property)
{
    DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
    if (directoryEntry.Properties.Contains(property))
        return directoryEntry.Properties[property].Value.ToString();
    else
        return String.Empty;
}

Use like this:

var createdDate = principal.GetProperty("whenCreated");
Per Noalt