Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 5-Jul-16 2:55am
v2
Comments
F-ES Sitecore 5-Jul-16 8:23am    
Without knowing how you manage your authentication it's impossible to say.

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
 
Share this answer
 
Take a look: Query AD and get LastLogonTimeStamp[^]

VB.NET
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
 
Share this answer
 
Comments
debanjanroy 5-Jul-16 7:56am    
_duDe, PrincipalContext() says undefined. How to solve this?
Leo Chapiro 5-Jul-16 8:13am    
You have to add a reference to System.DirectoryServices.AccountManagement.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900