Monday, 3 March 2008

AD When does my Password Expire?

We are implementing a new password policy and we want to show users when is their password to expire. (Something simple I know...).

Well with a bit of googling I have found this great post by Ryan Dunn

Using the PasswordExpires class from the post, and a bit of refactoring on the main class we can quickly get our personalize expiry page:

protected void Page_Load(object sender, EventArgs e)
PasswordExpires pe = new PasswordExpires();
string[] a = Context.User.Identity.Name.Split('\\');
string userName = a[1];

Response.Write(String.Format("Password Policy: {0} days", 0 - pe.PasswordAge.Days));

TimeSpan t = pe.WhenExpires(userName);
if (t == TimeSpan.MaxValue)
Response.Write(String.Format("{0}: Password Never Expires", userName));
else if (t == TimeSpan.MinValue)
Response.Write(String.Format("{0}: Password Expired", userName));
String.Format("Password for {0} expires in {1} days at {2}", userName, t.Days, DateTime.Now.Add(t)));