Bob
Bob

Reputation: 1061

PrincipalContext & UserPrincipal how to know when password expires?

I have a UserPrincipal object with a lot of properties, but I cannot find a property for the date that the password expires.

How can this be done?

Upvotes: 11

Views: 9536

Answers (1)

Drew Chapin
Drew Chapin

Reputation: 7989

This is the simplest approach I was able to come up with...

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using ActiveDs;

//...

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(domain, "username");
DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
IADsUser native = (IADsUser)entry.NativeObject;
Console.WriteLine(user.GivenName + "'s password will expire on " + native.PasswordExpirationDate);


Note #1: ActiveDs is listed on the COM tab of the Add Reference dialog as Active DS Type Library

Note #2: As far as I can tell, the PasswordExpirationDate is in UTC time.

Upvotes: 13

Related Questions