Problem with getting lastlogon for account - C#.NET / AD
Hi guys!
I have a code:
StringLastlogon(string username,string domain)
{
try
{
{
DirectoryContext context =newDirectoryContext(DirectoryContextType.Domain,domain);
DateTime latestLogon =DateTime.MinValue;
string servername =null;
DomainControllerCollection dcc =DomainController.FindAll(context);
foreach(DomainController dc in dcc)
{
DirectorySearcher ds;
using(dc)
using(ds = dc.GetDirectorySearcher())
{
ds.Filter=String.Format("(sAMAccountName={0})",username);
ds.PropertiesToLoad.Add("lastLogon");
ds.SizeLimit=1;
SearchResult sr = ds.FindOne();
if(sr !=null)
{
DateTimelastLogon=DateTime.MinValue;
if(sr.Properties.Contains("lastLogon"))
{
lastLogon=DateTime.FromFileTime((long)sr.Properties["lastLogon"][0]);
}
if(DateTime.Compare(lastLogon, latestLogon)>0)
{
latestLogon =lastLogon;
servername = dc.Name;
}
}
}
}
return latestLogon.ToString();
}
}
catch(Exception)
{
err =true;
returnnull;
}
}
posted @
2012-10-24 08:24
sirili
阅读(
348)
评论()
编辑
收藏
举报