如何使用VB.NET或C#获取当前用户的上次登录和注销时间 [英] How to get last login and logout time of current user by using VB.NET or C#

查看:126
本文介绍了如何使用VB.NET或C#获取当前用户的上次登录和注销时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取最后一次用户登录和注销时间并将它们存储到SQL Server 2008中。如何实现。我已经做了很多尝试来获取数据,但它并不富有成效,请指导我如何获取数据。



我尝试了什么:



我尝试过命令'net user'使用cmd,得到命令的输出,但它没有给我正确的输出。

I want to fetch the last user logon and logout time and store them into SQL server 2008. How it will be possible. I have done so many attempts to fetch the data but its not fruitful, please guide me how to fetch the data.

What I have tried:

I have tried using cmd by a command 'net user', and get the output of the command, but its not give me the proper output.

推荐答案





使用PrincipalContext和UserPrincipal类。 MSDN足以让你完成它。



主要内容类(System.DirectoryServices.AccountManagement) [ ^ ]



UserPrincipal Class(System.DirectoryServices) .AccountManagement) [ ^ ]



问候
Hi,

By using the PrincipalContext and UserPrincipal classes. The MSDN holds enough for you to be able to get it done.

PrincipalContext Class (System.DirectoryServices.AccountManagement)[^]

UserPrincipal Class (System.DirectoryServices.AccountManagement)[^]

Regards


看看: 查询AD并获取LastLogonTimeStamp [ ^ ]



Take a look: Query AD and get LastLogonTimeStamp[^]

Dim SearchContext As New PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, <DomainName>)
 Dim ADUser As New UserPrincipal(SearchContext)
 Dim ADSearcher As New PrincipalSearcher()
 Dim LastLogonDateTimeUTC As DateTime

 ADUser.Name = "*"
 ADSearcher.QueryFilter = ADUser
 Dim SearchResult As PrincipalSearchResult(Of Principal) = ADSearcher.FindAll()
 For Each User As UserPrincipal In SearchResult
  Console.WriteLine("Name: " & User.Name)
  If User.LastLogon.HasValue Then
   LastLogonDateTimeUTC = User.LastLogon
   Console.WriteLine("Last Logon: " & LastLogonDateTimeUTC.ToLocalTime)
  End If
 Next User


如果您正在谈论Windows登录和注销,这将比您想象的要困难得多。



只要用户登录Windows或使用需要身份验证的共享资源(例如将驱动器映射到共享,打开UNC或将内容打印到Windows托管的共享),就会发生登录打印机。这些可以由网络上的任何域控制器进行身份验证,具体取决于具体情况。



Windows默认情况下不会跟踪注销,除非您查看安全日志在用户注销的工作站上。没有跟踪注销的中央存储库。
If you're talking about Windows logins and logouts, this is going to be much harder than you think.

A login occurs whenever a user either logs into Windows or uses a shared resource that requires authentication, like mapping a drive to a share, opening up a UNC, or printing something to a Windows-hosted shared printer. These can be authenticated by any domain controller on the network, depending on circumstances.

Windows, by default, doesn't track logouts, unless you look at the security log on the workstation the user logged out of. There is no central repository that tracks logouts.


这篇关于如何使用VB.NET或C#获取当前用户的上次登录和注销时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆