Search code examples
winapiauthenticationlocal-security-authority

WIN32 - Last user to login


Is there a reliably way to determine the last user name to login to the system? I've looked at LsaEnumerateLogonSessions() and LsaGetLogonSessionData() however they require elevation on Vista and later (which I'm keen to avoid). WMI has the same problem (presumably it's just calling Lsa behind the scenes).

I've also looked at "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser" in HKLM, but in testing that's unreliable and doesn't get updated.

I'm really only interested in console logons, rather than Fast User Switching or TS logons.

I've read various articles, but have yet to come up with a good solution.


Solution

  • You might be able to use Audit Logon events - this requires your service to have a user access right to see the Security log, but not be a full Administrator.

    Eventid 528 indicates who logged on, you'd have to find the most recent instance of this.