我試圖接收當前連接到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'? –