Welcome to MSDN Blogs Sign in | Join | Help

Decrypt my World

Cryptography, Security, Debugging and more!

News

  • Any of my posts is supported under any Microsoft standard support program or service. They are provided "AS IS" without warranty of any kind, and confer no rights.

Where are my readers?

Locations of visitors to this page

Favorite Posts

How to get Password Expiration Date with System.DirectoryServices (C#)

Hi, welcome back,

You may want to get Password Expiration Date for a given user with System.DirectoryServices. You may be tempted to use a code like the following:

DirectoryEntry entry = new DirectoryEntry(path);
object obj = entry.Properties["PasswordExpirationDate"].Value;
DateTime passwordExpirationDate = (DateTime)obj;

But "obj" is always null even if a "net user" command returns a valid password expiration date.

We can check that PasswordExpirationDate is not an available property in DirectoryEntry.Properties.PropertyNames collection.

So we could use a code like the following instead, which works:

DirectoryEntry entry = new DirectoryEntry(path)
ActiveDs.IADsUser native = (ActiveDs.IADsUser)entry.NativeObject;
DateTime passwordExpirationDate = native.PasswordExpirationDate;

I hope this helps.

Regards,

 

Alex (Alejandro Campos Magencio)

Posted: Tuesday, March 25, 2008 5:05 PM by alejacma

Comments

xparrot said:

I used similar code to create a c# console application that will email users when their password is ready to expire.  You can just run it as a scheduled task. The code is posted on my blog:

http://houseofderek.blogspot.com/2008/07/password-expiration-email-utility.html

-xparrot

# July 11, 2008 3:37 PM

Thomas Holland said:

This method does not work.  

The method posted by xparrot DOES work.  I posted on it as well after spending a day figuring it out.  Some of the code comes from xparrot's excellent post.

http://dis.mantle.me/?p=106

# March 27, 2009 10:47 PM

alejacma said:

My method will work if PasswordExpirationDate contains some value of course. I and some customers of mine have used this successfully in the past. Of course, it will depend on your environment. If PasswordExpirationDate contains no value even after using NativeObject, then you have to use another method like xparrot's to find out when your password expires.

Thx,

Alex

# March 28, 2009 5:06 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker