I have been trying to print the lastLogin time from active directory, its taken about an hour of coding testing and googling, but I have done it.
First you have to get your AD entry. Now I work in a place with a few domains so I narrowed my search down a bit by domain, but I bet you can figure out how to tweak it.
Then there is some funky code to convert the AD largeint into a date time and there you are all done
(This is an example so as for naming and exception handling i recommend you do it right)
string login=”funky\simon”;
string[] keys= {“displayName”,”LastLogon”,”mail”};
string domain,name;domain=login.Split(‘\’)[0];
name=login.Split(‘\’)[1];System.DirectoryServices.DirectoryEntry o=
new System.DirectoryServices.DirectoryEntry(“LDAP://dc=” + s + “,dc=binaryjam,dc=com”);System.DirectoryServices.DirectoryEntry p=null;
System.DirectoryServices.DirectorySearcher search=
new System.DirectoryServices.DirectorySearcher(o);search.Filter = String.Format(“(SAMAccountName={0})”, name);
foreach (string key in keys)
{
search.PropertiesToLoad.Add(key);
}p=search.FindOne().GetDirectoryEntry();
foreach (string key in keys) {
string adDets=key + ” – “;
try {
if(key==”LastLogon”) {
ActiveDs.IADsLargeInteger li=((ActiveDs.IADsLargeInteger)(p.Properties[key].Value));
long date = (long)li.HighPart << 32 | (uint ) li.LowPart;
DateTime time = DateTime.FromFileTime( date );
adDets+=time.ToString();
}
else {
adDets+=p.Properties[key].Value.ToString();
}//Here I add asDets to a listBox but you do what you like with it
}
catch {//Yes I am a bad man, you do it right}}
I have the followng articles to thank
http://www.codeproject.com/dotnet/QueryADwithDotNet.asp?print=true
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=321717&SiteID=1