We can retrieve the accountExpires property for the account.
Account-Expires
The date when the account expires. This value represents the number of 100 nanosecond intervals since January 1, 2026 (UTC). A value of 0 or 0x7FFFFFFFFFFFFFFF (9223372036854775807) indicates that the account never expires.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/adschema/a_accountexpires.asp
And then we can calculate by comparing to today's time.
Here are some code, you may tune based on your request.
http://www.codecomments.com/archive291-2004-3-163563.html
NOTE: the LargeInteger is a AD type, so we need to add reference to the ActiveDs COM Lib in addition to System.DirectoryServices.
See this code;
using System.DirectoryServices;
// You must add a reference to the activeds.tlb in the system32 directory. (click, website > add reference)
// Declare that you are using the ActiveDs namespace.
using ActiveDs;
public
partial class StratAccountInfo : System.Web.UI.Page
{
private static string LdapADpath = ConfigurationManager.AppSettings["LdapString"];
protected void Page_Load(object sender, EventArgs e)
{
string LogUser = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1).ToString();
lblLoggedInAs.Text = LogUser;
DirectoryEntry entry = new DirectoryEntry(LdapADpath);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + LogUser + ")";
SearchResult LDAPresult = search.FindOne();
entry = LDAPresult.GetDirectoryEntry();
// Pulling the informtion on when the password was last changed and converting it to a LargeInteger.
LargeInteger liAcctPwdChange = entry.Properties["pwdLastSet"].Value as LargeInteger;
// Convert the highorder/loworder parts of the property pulled to a long.
long dateAcctPwdChange = (((long)(liAcctPwdChange.HighPart) << 32) + (long)liAcctPwdChange.LowPart);
// Convert FileTime to DateTime and get what today's date is.
DateTime dtNow = DateTime.Now;
// I added 90 days because I know what my password expiration is set to, if not you need to pull that information and add the number of days it is set for.
DateTime dtAcctPwdChange = DateTime.FromFileTime(dateAcctPwdChange).AddDays(90);
string strAcctPwdChange = DateTime.FromFileTime(dateAcctPwdChange).ToShortDateString();
string strAcctPwdExpires = DateTime.FromFileTime(dateAcctPwdChange).AddDays(90).ToShortDateString();
// Calculate the difference between the date the pasword was changed, and what day it is now and display the # of days.
TimeSpan time;
time = dtAcctPwdChange - dtNow;
lblPwdChangedDate.Text = strAcctPwdChange;
lblPassExp.Text = strAcctPwdExpires;
lblPwdExpDays.Text= time.Days.ToString() + " day(s)";
}
}