I have tried to find C# Framework 3.5/4.0 methods to determine when a user password in Active Directory will expire, but cannot seem to find anything.
I understand that in order to determine when a password expires, one must first get the policy from the Active Directory domain. I cannot seem to find any updated examples of how this is accomplished.
The UserPrinciple object has alot of useful properties that let me know IF the password is expired, but none to tell me when it will expire. Is there a good example anywhere?
Lee Z
Member
14 Points
25 Posts
C# Framework 3.5/4.0 method to determine when user password in Active Directory will expire
Sep 26, 2011 02:33 AM|LINK
Hello,
I have tried to find C# Framework 3.5/4.0 methods to determine when a user password in Active Directory will expire, but cannot seem to find anything.
I understand that in order to determine when a password expires, one must first get the policy from the Active Directory domain. I cannot seem to find any updated examples of how this is accomplished.
The UserPrinciple object has alot of useful properties that let me know IF the password is expired, but none to tell me when it will expire. Is there a good example anywhere?
Thanks,
Lee
kushal.dwive...
Member
396 Points
61 Posts
Re: C# Framework 3.5/4.0 method to determine when user password in Active Directory will expire
Sep 26, 2011 10:47 AM|LINK
You can find the PasswordExpirationDate from the underlying native COM object. Here is a sample code for it :
DirectoryEntry entry = new DirectoryEntry("LDAP://CN=username,OU=TestOU,DC=mydomain,DC=com"); // Connection string of user account
ActiveDs.IADsUser native = (ActiveDs.IADsUser)entry.NativeObject;
DateTime passwordExpirationDate = native.PasswordExpirationDate;
Please add the references to System.DirectoryServices and ActiveDS type library.
These forum posts will be of help to understand the other methods to calculate PasswordExpirationDate :
http://forums.asp.net/p/1235127/2241825.aspx
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0022c46f-1836-4e44-bfce-8c77553ed8d2/
http://msdn.microsoft.com/en-us/library/ms974598.aspx
Lee Z
Member
14 Points
25 Posts
Re: C# Framework 3.5/4.0 method to determine when user password in Active Directory will expire
Sep 26, 2011 05:57 PM|LINK
Thanks Kushal,
My code is provided
using (UserPrincipal user = GetUserPrincipal(username)) { if (user != null) { System.DirectoryServices.DirectoryEntry entry = (System.DirectoryServices.DirectoryEntry)user.GetUnderlyingObject(); ActiveDs.IADsUser native = (ActiveDs.IADsUser)entry.NativeObject; DateTime passwordExpirationDate = native.PasswordExpirationDate; if (!user.PasswordNeverExpires) { if (DateTime.Compare(passwordExpirationDate, DateTime.Now) <= 0) { status = true; } } } }kushal.dwive...
Member
396 Points
61 Posts
Re: C# Framework 3.5/4.0 method to determine when user password in Active Directory will expire
Sep 27, 2011 04:29 AM|LINK
You are welcome Lee. Happy to help :)