I have a daemon service in.NetCore 3.1. It is executed by Windows TaskScheduler. It needs to capture the window's current logged-in username. How to do it? Tried few options-
Both above are not giving logged-in username. When I execute the daemon service manually, then only they give the correct username.
WindowsIdentity.GetCurrent().Name -> It is always returning SYSTEM as username when daemon service is executed by TaskScheduler.
Environment.UserName -> It is always returning Windows Hostname as username when daemon service is executed by TaskScheduler.
Kindly suggest!
As Damien mentioned in comments, the current user can be interpreted differently. No one needs to be logged in either.
You can try this way:
Retrieves the session identifier of the console session
[DllImport("kernel32.dll")]
static extern uint WTSGetActiveConsoleSessionId ();
// obtain the currently active session id; every logged on user in the system has a unique session id
uint dwSessionId = WTSGetActiveConsoleSessionId();
https://smbadiwe.github.io/post/track-activities-windows-service/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.