简体   繁体   English

如何查找哪个用户最后通过C#中的Active Directory登录到给定的计算机?

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

I am trying to programmatically find who last logged onto a given computer and when with C#. 我试图以编程方式查找谁最后一次登录到给定计算机以及何时使用C#。 Given the name of a computer as a string, I have learned about Getting last Logon Time on Computers in Active Directory . 给定计算机名称作为字符串,我了解了有关Active Directory中的计算机上次登录时间的获取方法 However, there doesn't seem to be a property for which user was the one that actually logged in. Do I have to take a different approach for this? 但是,似乎没有用于实际登录的用户的属性。为此,我是否需要采取其他方法? Anything I found online that was remotely related to this was in VBScript, but this must be done in C#. 我在网上发现的与此远程相关的所有内容都在VBScript中,但这必须在C#中完成。

Simply query the necessary information from the System Registry. 只需从系统注册表中查询必要的信息。 The following method will set the Registry View based on whether the machine is 64-bit or 32-bit - although if you're doing this remotely - then the approach to obtain this information may need to be altered, but the general approach should be the same. 以下方法将基于计算机是64位还是32位来设置注册表视图-尽管如果您是远程执行的,则可能需要更改获取此信息的方法,但是应该更改常规方法相同。

The Base Key is selected using the name of the machine that you pass an argument along with the Registry View and of course the Registy Hive as Local Machine. 使用您将参数与“注册表视图”一起传递的计算机的名称以及“ Registy Hive作为本地计算机”的计算机名称来选择基本密钥。 Then you open up the Base Key and finally the necessary Sub Key where the information you desire resides. 然后,您打开基本密钥,最后打开所需的子密钥,以保留所需的信息。

The location where that information is contained is: 包含该信息的位置是:

SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI 软件\\ Microsoft \\ Windows \\ CurrentVersion \\ Authentication \\ LogonUI

And from there grab the value from LastLoggedOnUser . 然后从那里获取LastLoggedOnUser的值。

Here is the code in C#: 这是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;
        }
    }
}

Here is some code I found: 这是我发现的一些代码:

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();

Here is the website where all of this code came from. 这是所有这些代码的来源网站。 The developer explained the code in detail. 开发人员详细解释了代码。 http://www.codeproject.com/Articles/19181/Find-LastLogon-Across-All-Windows-Domain-Controlle http://www.codeproject.com/Articles/19181/Find-LastLogon-Across-All-Windows-Domain-Controlle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到计算机所属的组织单位? (Active Directory C#) - How can I find what Organizational Units a computer is part of? ( Active Directory C# ) 如何从C#中找到活动目录中的用户? - How can you find a user in active directory from C#? c#Active Directory身份验证用户(如果计算机不在域中) - c# Active Directory Authentication User if Computer not in domain C#从Active Directory,IIS中的登录用户获取登录用户的电子邮件地址 - C# Get Logged in user email address from logged in user in Active Directory, IIS 如何使用c#从Active Directory中删除计算机帐户 - how to delete computer account from Active Directory using c# 如何通过 C# 从 azure 活动目录获取上次登录日期和创建日期 - how to get the last login date and created date from azure active directory through C# 如何在 C# 中的 Active Directory 中验证用户 - How to validate the user in Active Directory in C# 使用C#禁用Active Directory中的计算机 - Use C# to disable a computer in Active Directory 如何使用 C# 在 Active Directory 中查找给定的键值 (web.config) - How to find given key values (web.config) in Active Directory using C# 如何使用 C# 在 Active Directory 中搜索用户的姓氏和名字 - How can I search for both the user's last name AND the first name in the Active Directory using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM