11

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?

1 Answer 1

13

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.

5
  • Great answer. thanks! a tip - sometimes, using FindByIdentity is very slow. mostly on local machines. in these cases, it's better to use PrincipalSearcher
    – itsho
    Jul 3, 2016 at 21:39
  • I don't normally use the "Principal" classes. I prefer the DirectoryEntry and DirectorySearcher objects. In my tests they tend to be faster. Jul 3, 2016 at 23:43
  • In which assembly does ActiveDs live?
    – Bartosz
    Jun 26, 2017 at 15:01
  • This works, however also makes dozens of COM-related warnings show up in the build output in Visual Studio (MSB3305 warnings), which I can't disable from my side.
    – Talset
    Dec 4, 2019 at 22:08
  • If PasswordExpirationDate = 01/01/1970 ??
    – Kiquenet
    Apr 15, 2020 at 10:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.