簡體   English   中英

如何查找哪個用戶最后通過C#中的Active Directory登錄到給定的計算機?

[英]How to find what user last logged onto a given computer through Active Directory in C#?

我試圖以編程方式查找誰最后一次登錄到給定計算機以及何時使用C#。 給定計算機名稱作為字符串,我了解了有關Active Directory中的計算機上次登錄時間的獲取方法 但是,似乎沒有用於實際登錄的用戶的屬性。為此,我是否需要采取其他方法? 我在網上發現的與此遠程相關的所有內容都在VBScript中,但這必須在C#中完成。

只需從系統注冊表中查詢必要的信息。 以下方法將基於計算機是64位還是32位來設置注冊表視圖-盡管如果您是遠程執行的,則可能需要更改獲取此信息的方法,但是應該更改常規方法相同。

使用您將參數與“注冊表視圖”一起傳遞的計算機的名稱以及“ Registy Hive作為本地計算機”的計算機名稱來選擇基本密鑰。 然后,您打開基本密鑰,最后打開所需的子密鑰,以保留所需的信息。

包含該信息的位置是:

軟件\\ Microsoft \\ Windows \\ CurrentVersion \\ Authentication \\ LogonUI

然后從那里獲取LastLoggedOnUser的值。

這是C#中的代碼:

private static string GetLastUserLoggedOn(string machineName)
{
    string location = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI";
    var registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
    using (var hive = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName, registryView))
    {
        using (var key = hive.OpenSubKey(location))
        {
            var item = key.GetValue("LastLoggedOnUser");
            string itemValue = item == null ? "No Logon Found" : item.ToString();
            return itemValue;
        }
    }
}

這是我發現的一些代碼:

using System;            
    // has DateTime class
using System.Collections.Generic;    
    // has the Dictionary class
using System.DirectoryServices;    
    // has all the LDAP classes such as DirectoryEntry 
using ActiveDs;            
    // has the IADsLargeInteger class


// Get the root entry
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string configurationNamingContext = 
    (string)rootDSE.Properties["configurationNamingContext"].Value;
string defaultNamingContext = 
    (string)rootDSE.Properties["defaultNamingContext"].Value;
// Get all the domain controllers


// Get all the domain controllers
DirectoryEntry deConfig = 
    new DirectoryEntry("LDAP://" + configurationNamingContext);
DirectorySearcher dsConfig = new DirectorySearcher(deConfig);
dsConfig.Filter = "(objectClass=nTDSDSA)";
foreach (SearchResult srDomains in dsConfig.FindAll()) 
{
    DirectoryEntry deDomain = srDomains.GetDirectoryEntry();
    if (deDomain != null) 
    {
        string dnsHostName = 
            deDomain.Parent.Properties["DNSHostName"].Value.ToString();
        // Get all the users for that domain
    }
}


// Get all the users for that domain
DirectoryEntry deUsers = 
    new DirectoryEntry("LDAP://" + dnsHostName + "/" + defaultNamingContext);
DirectorySearcher dsUsers = new DirectorySearcher(deUsers);
dsUsers.Filter = "(&(objectCategory=person)(objectClass=user))";
foreach (SearchResult srUsers in dsUsers.FindAll()) 
{
    DirectoryEntry deUser = srUsers.GetDirectoryEntry();
    if (deUser != null) 
    {
        // Get the distinguishedName and lastLogon for each user
        // Save the most recent logon for each user in a Dictionary object
    }
}

//Create Dictionary
Dictionary<string, Int64> lastLogons = new Dictionary<string, Int64>();


// Get the distinguishedName and lastLogon for each user
string distinguishedName = 
    deUser.Properties["distinguishedName"].Value.ToString();
Int64 lastLogonThisServer = new Int64();
if (deUser.Properties["lastLogon"].Value != null) 
{
    IADsLargeInteger lgInt = 
        (IADsLargeInteger)deUser.Properties["lastLogon"].Value;
    lastLogonThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;
}

// Save the most recent logon for each user in a Dictionary object
if (lastLogons.ContainsKey(distinguishedName)) 
{
    if (lastLogons[distinguishedName] < lastLogonThisServer) 
    {
        lastLogons[distinguishedName] = lastLogonThisServer;
    }
} 
else 
{
    lastLogons.Add(distinguishedName, lastLogonThisServer);
}


//Get the time
// Convert the long integer to a DateTime value
string readableLastLogon = 
    DateTime.FromFileTime(lastLogonThisServer).ToString();

這是所有這些代碼的來源網站。 開發人員詳細解釋了代碼。 http://www.codeproject.com/Articles/19181/Find-LastLogon-Across-All-Windows-Domain-Controlle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM