获取本地Windows用户登录会话时间戳在C#

编程入门 行业动态 更新时间:2024-10-23 15:21:49
本文介绍了获取本地Windows用户登录会话时间戳在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在试图寻找周围的各种.NET类库的一些在那里我可以得到记录在本地机器的用户,无论是连接到域或没有。 到目前为止

I've been trying to look around the various .NET class library's for some where I can get the logged in user of the local machine, either connected to a domain or not. So far

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

这将返回用户的名称,但有什么办法获得会话细节的东西,你会在AD或用户看到登录,会话持续时间等。用户的情况下,如工作站操作锁定中,用户basiclly的presence。

This will return the user's name, however is there any way of getting the session details, something that you would see in AD or user logged in, session duration, etc.. the context of the user, actions such as Workstation locked, the presence of the user basiclly.

如果您有任何想法,这将是更AP preciated。 先谢谢了。

If you have any idea, it would be much appreciated. Thanks in advance.

推荐答案

您可以查询很多数据的Active Directory,您使用的 System.DirectoryServices中命名空间。例如,下面的示例显示了用户的最后一次登录时间。

You can query Active Directory for much of the data that you need through LDAP queries using the System.DirectoryServices namespace. For example, the sample below shows the user's last logon time.

当然,这仅适用于域用户。

Of course, this only works for domain users.

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

更多推荐

获取本地Windows用户登录会话时间戳在C#

本文发布于:2023-11-06 06:55:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户登录   时间   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!