none
Get Log on Username in system session .. RRS feed

  • Question

  • Hi,

    I'm developing a windows service and i need to get log on username but the problem is service is running under system account so it always return system as username, is there any way to get log on username thru service which is running under system account ?? I'm using windows 7

    Thanks and regards.

    Wednesday, March 18, 2015 5:25 AM

Answers

  • Since windows services run in Session 0, I don't think it'll show what Wall.E wants.

    On the other hand, I forgot that the current console user can be found on Win32_ComputerSystem as well. I've tested the following code can get console user name on service.

            private string GetConsoleUser()
            {
                string username = String.Empty;
    
                ManagementScope scope = new ManagementScope("\\\\.\\root\\cimv2");
    
                ManagementObjectCollection compQuery = new ManagementObjectSearcher(scope, new SelectQuery("select * from Win32_ComputerSystem")).Get();
                foreach (ManagementObject item in compQuery)
                {
                    username = Convert.ToString(item["username"]);
                }
    
                return username;
            }


    • Edited by cheong00Editor Thursday, March 19, 2015 6:25 AM
    • Marked as answer by Wall.E Thursday, March 19, 2015 7:33 AM
    Thursday, March 19, 2015 6:24 AM
    Answerer

All replies

  • You can use WMI to query Win32_LogonSession.
    Thursday, March 19, 2015 1:56 AM
    Answerer
  • Hello Wall,

    If you use .NET, you could have a try use below code to see if works:

    string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 19, 2015 5:35 AM
    Moderator
  • Since windows services run in Session 0, I don't think it'll show what Wall.E wants.

    On the other hand, I forgot that the current console user can be found on Win32_ComputerSystem as well. I've tested the following code can get console user name on service.

            private string GetConsoleUser()
            {
                string username = String.Empty;
    
                ManagementScope scope = new ManagementScope("\\\\.\\root\\cimv2");
    
                ManagementObjectCollection compQuery = new ManagementObjectSearcher(scope, new SelectQuery("select * from Win32_ComputerSystem")).Get();
                foreach (ManagementObject item in compQuery)
                {
                    username = Convert.ToString(item["username"]);
                }
    
                return username;
            }


    • Edited by cheong00Editor Thursday, March 19, 2015 6:25 AM
    • Marked as answer by Wall.E Thursday, March 19, 2015 7:33 AM
    Thursday, March 19, 2015 6:24 AM
    Answerer
  • thanks for the reply Fred, yes it stands valid if the application is also running in the same session e.g. service is running under system account so it always return System not the interactive log on user.
    Thursday, March 19, 2015 7:32 AM
  • thanks for help cheng00 it works :)
    Thursday, March 19, 2015 7:32 AM