4

我一直在尝试查看各种 .NET 类库,在其中可以获取本地计算机的登录用户,无论是否连接到域。至今

System.Security.Principal.WindowsPrincipal LoggedUser = System.Threading.Thread.CurrentPrincipal as 
System.Security.Principal.WindowsPrincipal;
// This returns the username
LoggedUser.Identity.Name

这将返回用户的姓名,但是是否有任何方法可以获取会话详细信息,您将在 AD 中看到的内容或用户登录、会话持续时间等。用户的上下文、工作站锁定等操作、存在用户的基本。

如果您有任何想法,将不胜感激。提前致谢。

4

2 回答 2

2

您可以使用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();
        }
    }
}
于 2009-03-25T17:12:02.903 回答
1

您可以从 WMI 查看WMI_LogonSession获取其中一些信息,例如开始时间

于 2009-03-25T16:35:40.823 回答