0

Possible Duplicate:
How do you retrieve a list of logged-in/connected users in .NET?

I have hosted WCF service in IIS on a machine. Now i want to write a method in the service by which I could detect whether the hosted machine is currently being used by any Remote Desktop Connection or not.

Is there any other better way to find out this, and what are the exact code APIs by which RDC connection is detected.

I am using C#.net, framework 3.5

Community
  • 1
  • 1
M_Idrees
  • 2,080
  • 2
  • 23
  • 53

2 Answers2

2

There are two options for you. You can use the P/Invoke Calls to the WSTAPI library however it is easy to mess stuff up and get resource leaks.

Your other option is to use the WTSAPI wrapper library Cassia. I have used that before and it is easy to use and the classes are fairly self documenting.

In Cassia your function would simply be

public bool IsComputerUsedByTS()
{
    var tsMgr = new TerminalServicesManager();
    var localSvr = tsMgr.GetLocalServer();
    var sessions = localSvr.GetSessions();
    foreach(var session in sessions)
    {
        if(session.ConnectionState == ConnectionState.Active || 
           session.ConnectionState == ConnectionState.Connected) //Add more states you want to check for as needed
        {
            return true;
        }
    }
    return false;
}

I did not test the above, and I think "active" is used if you are a console session where "Connected" is for RDP sessions. there are lot more states, look at the enum for all of the options.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • The `Active` status is actually used both by the console session and remote sessions and is the status you're really looking for. The `Connected` status is less common, and I've only seen it briefly when a session is being initially set up by Windows (and perhaps when the user is on the logon screen when Network Level Authentication is not being used on the RD connection). – Dan Ports Dec 01 '12 at 19:09
  • As I said, I did not have a TS server in front of me to test on. Thanks for the info. – Scott Chamberlain Dec 01 '12 at 20:01
  • Thanks for the answer - you got me out of a serious jam! One little change: `GetSessions()` should be `localSvr.GetSessions()` – Matt Harley Jul 18 '13 at 07:33
1

RDP servers listen on port 3389 by default (but can be configured differently).

Therefore (even not being 100% reliable) this can be used to check any active RDP connections.

bool inUse = IPGlobalProperties.GetIPGlobalProperties()
              .GetActiveTcpConnections()
              .Any(tcp => tcp.LocalEndPoint.Port == 3389);

EDIT

I added @t3hn00b's suggestion.

int port = 3389;
using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", false))
{
    if (key != null)
    {
        object value = key.GetValue("PortNumber");
        if (value != null) port = Convert.ToInt32(value);
    }
}

namespace: System.Net.NetworkInformation

L.B
  • 114,136
  • 19
  • 178
  • 224
  • Regarding the port part - there's a registry key that can be used to get the port `HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet > Control > Terminal Server > WinStations > RDP-Tcp > PortNumber` – t3hn00b Nov 29 '12 at 08:19
  • @t3hn00b you are right. It can used for 3389 constant – L.B Nov 29 '12 at 08:29