您可以使用System.DirectoryServices命名空间通过 LDAP 查询查询 Active Directory 以获取您需要的大部分数据。例如,下面的示例显示了用户的上次登录时间。
当然,这只适用于域用户。
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace ADMadness
{
class Program
{
static void Main(string[] args)
{
DirectorySearcher search = new DirectorySearcher("LDAP://DC=my,DC=domain,DC=com");
search.Filter = "(SAMAccountName=MyAccount)";
search.PropertiesToLoad.Add("lastLogonTimeStamp");
SearchResult searchResult = search.FindOne();
long lastLogonTimeStamp = long.Parse(searchResult.Properties["lastLogonTimeStamp"][0].ToString());
DateTime lastLogon = DateTime.FromFileTime(lastLogonTimeStamp);
Console.WriteLine("The user last logged on at {0}.", lastLogon);
Console.ReadLine();
}
}
}