C# .NET - How to get password expiration date for a user in Active directory

Asked By Dot Net Programmer on 26-Oct-09 06:43 AM

Dear Friends,

I want to display the password expiration date or last date of password changed in a form.

Please help me to get this solution.


Regards,

lovedotnet

password expiration date for a user in Active directory

mv ark replied to Dot Net Programmer on 26-Oct-09 06:55 AM
Try the code at this link to get password expiration date for a user in Active directory -
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0022c46f-1836-4e44-bfce-8c77553ed8d2

get password expiration date for a user in Active directory

Sagar P replied to Dot Net Programmer on 26-Oct-09 07:04 AM

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)";
}
}

Re

Dot Net Programmer replied to Sagar P on 26-Oct-09 07:26 AM

Dear Sujit

Can u please tell me what needs to be given for below code. I mean in LdapString what we have to give and where we need to write.

private static string LdapADpath = ConfigurationManager.AppSettings["LdapString"];

Regards,

lovedotnet

It a path server
Sagar P replied to Dot Net Programmer on 26-Oct-09 09:28 AM

NET C# LDAP library provides easy access to any LDAP compliant directory from managed code. The library enables the developers to write LDAP enabled applications that access, manage, and update information stored in Novell eDirectory or other LDAP-aware directories. We assume that the user of the library is familiar with general understanding of LDAP before using the class provided in the library.

So you need to provide it something like;

string Path = "LDAP://LDAPServer/o=company";

in your app.config file. So that in code you can use it in DirectoryEntry

RE
Dot Net Programmer replied to Sagar P on 27-Oct-09 05:56 AM

Dear Sujit,

Thanks for u r kind help. Following code will display both pwd changed date and exp date. But for this we need to provide domain user id and pwd. Is there any way to get pwd from AD for a particualar user id.

My Code:

public class PasswordExpDate : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Label Label2;

protected System.Web.UI.WebControls.Label Label3;

protected System.Web.UI.WebControls.Label Label4;

protected System.Web.UI.WebControls.Label Label1;

private static string LdapADpath = @LDAP://LDAPServerName;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//Label1.Text = GetMaxPasswordAge().ToString();

try

{

string LogUser = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1).ToString();

Label1.Text = "UserName: "+ LogUser;

//DirectoryEntry entry = new DirectoryEntry("LDAP://domain/CN=Administrator,cn=users,DC=celeb,DC=w2kdom,DC=com","xxx\\Administrator", "HO123456");

DirectoryEntry entry = new DirectoryEntry(LdapADpath),LogUser,"PWD");

entry.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = "(SAMAccountName=" + LogUser + ")";

search.PropertiesToLoad.Add("cn");

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;

Label2.Text ="PWD Changed Date: "+ strAcctPwdChange;

Label3.Text ="PWD Exp Date: "+ strAcctPwdExpires;

Label4.Text="Left Days: "+ time.Days.ToString() + " day(s)";

}

catch (Exception Ex)

{

Response.Write(Ex);

}

}


Regards,

lovedotnet