Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 480
Bob Avatar asked Apr 04 '11 07:04

Bob


People also ask

What is C# PrincipalContext?

PrincipalContext(ContextType) Initializes a new instance of the PrincipalContext class with the specified context type. PrincipalContext(ContextType, String) Initializes a new instance of the PrincipalContext class with the specified context type and name.

What does principal context mean?

What does the principal "context" mean? designing post-click experiences that are consistent to the data established prior to the click.

What is System DirectoryServices AccountManagement?

System. DirectoryServices. AccountManagement manages directory objects independent of the System.

What is UserPrincipal C#?

UserPrincipal(PrincipalContext) Initializes a new instance of the UserPrincipal class by using the specified context. UserPrincipal(PrincipalContext, String, String, Boolean) Initializes a new instance of the UserPrincipal class by using the specified context, SAM account name, password, and enabled value.


1 Answers

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.

like image 195
Drew Chapin Avatar answered Oct 12 '22 20:10

Drew Chapin