none
How Can I get current username in windows service?

    Question

  • Hi,

    I have a windows service. It will start and stop one exe. When the exe is running I am updating the details of the current system information into a notepad. It is working fine except username. If I use System.Environment.UserName it returns "SYSTEM" or If I use System.Security.Principal.WindowsIdentity.GetCurrent().Name it returns "NT AUTHORITY\SYSTEM". But I want to write current exact username. How can I do this task. It is very urgent for me.

    Thanks in Advance,
    Jayhar
    Thursday, April 30, 2009 6:50 AM

Answers

  • using System.Security.Principal;
    ......
    WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    String username = wp.Identity.Name;

    Or

    For UserName = Try System.Security.Principal.WindowsIdentity.GetCurrent().User

    For ComputerName= Try System.Security.Principal.WindowsIdentity.GetCurrent()Name


    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 7:08 AM
  • Well the service IS logged on as System, so that's what it returns.

    You can change the account with which a server logs on via the Services applet (reached via Administrative Tools -> Services).
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 8:32 AM
  • Hi,

    try using System.Security.Principal.WindowsIdentity.GetCurrent().Name

    hope this helps.

    Thanks
    SN Zeih ZeZen
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 10:25 AM
  • Think about what you're asking. A windows service is a program that runs outside of any logged in users (it can itself log in as a specific user, but has to be specified as such). From the point of view of the service, at any given time there can be zero or more users logged in. So let me ask you a question:

    What user?

    Now that you understand life from the service's point of view, perhaps you can explain the task you need to accomplish and someone here can suggest a solution.
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 2:30 PM

All replies

  • using System.Security.Principal;
    ......
    WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    String username = wp.Identity.Name;

    Or

    For UserName = Try System.Security.Principal.WindowsIdentity.GetCurrent().User

    For ComputerName= Try System.Security.Principal.WindowsIdentity.GetCurrent()Name


    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 7:08 AM
  • Hi,

     Same problem is happening for this code "UserName = Try System.Security.Principal.WindowsIdentity.GetCurrent().User". It returns "S-U-15". If I use WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    String username = wp.Identity.Name;
    It is returning always "SYSTEM". This is the problem when I run the exe from a windows service. Any idea?

    Thanks in Advance,
    Jayahar


    Thursday, April 30, 2009 8:15 AM
  • Well the service IS logged on as System, so that's what it returns.

    You can change the account with which a server logs on via the Services applet (reached via Administrative Tools -> Services).
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 8:32 AM
  • Hi,

    try using System.Security.Principal.WindowsIdentity.GetCurrent().Name

    hope this helps.

    Thanks
    SN Zeih ZeZen
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 10:25 AM
  • Hi,

    If we use this in normal form it is working. But If we call this exe from a windows service it is giving the output like "NT AUTHORITY\SYSTEM". I want current actual username when I run the exe from Windows service. Any idea?

    Thanks,
    Jayahar
    Thursday, April 30, 2009 12:46 PM
  • Think about what you're asking. A windows service is a program that runs outside of any logged in users (it can itself log in as a specific user, but has to be specified as such). From the point of view of the service, at any given time there can be zero or more users logged in. So let me ask you a question:

    What user?

    Now that you understand life from the service's point of view, perhaps you can explain the task you need to accomplish and someone here can suggest a solution.
    • Marked as answer by Harry Zhu Wednesday, May 6, 2009 6:49 AM
    Thursday, April 30, 2009 2:30 PM
  • Hi,

    The windos service will start and stop my console application's exe. When the exe is running I am updating the details of the current system information into a notepad. If I run the exe manually (not by the windows service) or debuging the program I can get the username friom these codings System.Environment.UserName or System.Security.Principal.WindowsIdentity.GetCurrent().Name. But when I run the exe by windows service I can get all the details except username (application writing username like "SYSTEM" or  "NT AUTHORITY\SYSTEM" for the above codings Ex: If I use System.Environment.UserName it returns "SYSTEM" or If I use System.Security.Principal.WindowsIdentity.GetCurrent().Name it returns "NT AUTHORITY\SYSTEM"). But I want to write current exact username (exe running on which users domain).  

    Thanks in Advance,
    Jayhar
    Thursday, May 7, 2009 5:59 AM
  • Let the console application accepts commandline parameters.

    From windows service pass System.Environment.UserName as command line argument.
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Thursday, May 7, 2009 6:03 AM
  • What causes the service to suddenly invoke your application?
    Thursday, May 7, 2009 1:18 PM
  • The following guide may be helpful for you:
    http://support.microsoft.com/kb/q308403/
    Monday, May 11, 2009 11:21 PM
  • You can try this code spinet. Whenever a user logged onto windows, the Username attribute will contain the username of the user. In the case when there are no users in the windows system, there will be no instances of the Win32_ComputerSystem class.

    ManagementScope ms = new ManagementScope("\\\\.\\root\\cimv2");
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);
    foreach(ManagementObject mo in searcher.Get())
    {
        Console.WriteLine(mo["UserName"].ToString());
    }

     

    Tola Chhoeun

    • Proposed as answer by Vannes Tuesday, July 24, 2018 1:27 AM
    Friday, June 17, 2016 11:51 PM
  • This way is simple and works for me. Good job!!

    Vannes Yang

    Tuesday, July 24, 2018 1:28 AM
  • Very nice solution !! it Works for me !! Congratulations !!
    Thursday, July 26, 2018 5:18 PM

  • I know the post is old, but I'm trying a little problem with this routine on some equipment. The return of the WMI COMPUTERSYSTEM.USERNAME sometimes comes null and in other equipment with the correct user. Have you taken an example like that?
    Friday, October 19, 2018 2:58 PM