4

I am running a Windows service under the Administrator account and I would like to find out who is the currently logged in user. It will be different from the account the service is currently running under. Is there any easy way to get it from C#?

Those do not appear to work:

  • System.Security.Principal.WindowsIdentity.GetCurrent()
  • System.Environment.UserName

I am interested who is currently logged in with a console session.

I am not interested in other remote sessions, since this is not my case scenario.

janw
  • 8,758
  • 11
  • 40
  • 62
Marek
  • 2,419
  • 6
  • 34
  • 38

5 Answers5

6

1) Cassia should be able to give you a list of currently logged in users including RDC.

foreach (ITerminalServicesSession sess in new TerminalServicesManager().GetSessions())
{
    // sess.SessionId
    // sess.UserName
}

2) WMI (SO answer)

Select * from Win32_LogonSession

3) PInvoke to WTSEnumerateSessions

4) Enumerate all instances of "explorer.exe" and get the owner using PInvoke (OpenProcessHandle).

Process[] processes = Process.GetProcessesByName("explorer");

This is a bit hacky. WMI can also be used for this.

It might be a good idea to set winmgmt as a dependency for your service if you decided to go with solution that uses WMI.

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70
2

You might want to look at Cassia:

Cassia is a .NET library for accessing the native Windows Terminal Services API (now the Remote Desktop Services API). It can be used from C#, Visual Basic.NET, or any other .NET language.

and:

Enumerating terminal sessions and reporting session information including connection state, user name, client name, client display details ...

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

Try this,

http://www.codeproject.com/KB/vb/Windows_Service.aspx

Pretty simple idea, just loops through active processes to find 'explorer.exe' and lists the user thats its running under. Might have to adjust if you have multiple users.

Kratz
  • 4,280
  • 3
  • 32
  • 55
0

Using the Local Security Authority to Enumerate User Sessions in .NET http://www.codeproject.com/KB/system/LSAEnumUserSessions.aspx

WTSQuerySessionInformation Function http://msdn.microsoft.com/en-us/library/aa383838%28v=vs.85%29.aspx

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
0

A list of users currently logged into console sessions can be retrieved via WMI. You'll need to add a reference to System.Management.dll:

public static List<string> GetLoggedOnUsers(CacheLevel level)
{
    const int ConsoleSession = 2;

    string logonQuery = "SELECT * FROM Win32_LogonSession WHERE LogonType = " + ConsoleSession;

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(logonQuery);

    List<string> userNames = new List<string>();

    foreach (ManagementBaseObject logon in searcher.Get())
    {
        string logonId = logon.Properties["LogonId"].Value.ToString();

        string userQuery = "ASSOCIATORS OF {Win32_LogonSession.LogonId=" + logonId + "} "
                           + "WHERE AssocClass=Win32_LoggedOnUser Role=Dependent";

        ManagementObjectSearcher searcher2 = new ManagementObjectSearcher(userQuery);

        foreach (ManagementBaseObject user in searcher2.Get())
        {
            string name = user.Properties["FullName"].Value.ToString();

            userNames.Add(name);
        }
    }

    return userNames.Distinct().ToList();
}
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130