我试图接收当前连接到AD的所有计算机,以及哪些用户已登录到AD。我已经尝试了ComputerPrincipal
的.LastLogon
属性,但我得到的价值完全是关闭的,大概一周。确定AD中的计算机是否有用户登录
我想知道AD中的哪些计算机可用。有另一种方法可以使用吗?
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "192.168.0.101:389", "Administrator", "XXXX");
// define a "query-by-example" principal - here, we search for a ComputerPrincipal
ComputerPrincipal qbeComputer = new ComputerPrincipal(ctx);
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeComputer);
// find all matches
foreach (var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
ComputerPrincipal cp = found as ComputerPrincipal;
if (cp != null)
{
string computerName = cp.Name;
DateTime? lastLogon = new DateTime();
lastLogon = cp.LastLogon;
DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(cp.LastLogon.ToString()), DateTimeKind.Utc);
var kind = convertedDate.Kind;
DateTime dt = convertedDate.ToLocalTime();
Response.Write(cp.Name+" : "+ dt.ToString()+ "<br />");
}
}
编辑:
我想打印出来是这样的:
电脑1:真 电脑2:假 电脑3:假 电脑4:真
查询电脑当前是否登录不可能?我只需要一个布尔,对或错。
提供的一个例子你得到了什么以及你的期望。 –
Active Directory是一个**静态**资源 - 它没有“动态”运行时信息,例如谁在哪台计算机上登录。恐怕您无法从Active Directory中检索您要查找的信息。 –
lastLogon发生了什么 - 为什么当您立即用'cp.LastLogin'替换'new DateTime()'时, '?同样,当你刚刚在前一行指定时,为什么要访问'convertedDate.Kind'? –