c# getting ip address of user who remotely connect to machine

3.8k views Asked by At

I am using following piece of c# code in windows service to capture Remote connect and disconnect events.

How I can get the IPaddress of end user who remotely connect to that machine.

protected override void OnSessionChange(SessionChangeDescription changeDescription)
 {
        switch (changeDescription.Reason)
        {
            case SessionChangeReason.RemoteConnect:
                //Remote Connect
                break;
            case SessionChangeReason.RemoteDisconnect:
                //Remote Disconnect
                break;
            default:
                break;
        }
 }
2

There are 2 answers

0
toughcanny On BEST ANSWER

I found related solution in c#, it's using PInvoke WTSQuerySessionInformation with WTS_INFO_CLASS.WTSClientAddress

For more details you can go here: Grabbing Information of a Terminal Services Session Programmatically

Note from Selvin: solution doesn't free returned buffer ... you sould call WTSFreeMemory(pAddress)

0
Hozikimaru On

Here is the answer to your question;

Detect source of remote desktop connection

It simply indicates that you can query the connections and parse them.