10

Is there a method in C# to retrieve the user name from a given session id?
(any session running on the system)

The Win API function WTSQuerySessionInformation does this, but I'm searching for this functionality in C#.

8
  • stackoverflow.com/questions/6061143/… Commented Oct 21, 2013 at 6:29
  • @ThirdBattleOfPanipat: This is more complicated than WTSQuerySessionInformation is - and it's also Win API, not plain C#. So there's no method in the .NET framework? Commented Oct 21, 2013 at 6:33
  • There is no concept of "user name" associated with ASP.Net sessions... What exactly "user name" you want to get? Commented Oct 21, 2013 at 7:06
  • @AlexeiLevenkov: This is used by a desktop allication, no ASP here. Commented Oct 21, 2013 at 7:11
  • .NET is focused on end-user and web applications, so as a general rule it excludes the more hard-core system management functions. I'd be surprised if it contained a terminal services API. Commented Oct 22, 2013 at 1:02

1 Answer 1

16

There seems to be no .NET integrated method for this.
So this is the current solution, using Windows Terminal services API:

    [DllImport("Wtsapi32.dll")]
    private static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WtsInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out int pBytesReturned);
    [DllImport("Wtsapi32.dll")]
    private static extern void WTSFreeMemory(IntPtr pointer);

    public enum WtsInfoClass
    {
        WTSInitialProgram,
        WTSApplicationName,
        WTSWorkingDirectory,
        WTSOEMId,
        WTSSessionId,
        WTSUserName,
        WTSWinStationName,
        WTSDomainName,
        WTSConnectState,
        WTSClientBuildNumber,
        WTSClientName,
        WTSClientDirectory,
        WTSClientProductId,
        WTSClientHardwareId,
        WTSClientAddress,
        WTSClientDisplay,
        WTSClientProtocolType,
        WTSIdleTime,
        WTSLogonTime,
        WTSIncomingBytes,
        WTSOutgoingBytes,
        WTSIncomingFrames,
        WTSOutgoingFrames,
        WTSClientInfo,
        WTSSessionInfo,
    }

    public static string GetUsernameBySessionId(int sessionId, bool prependDomain) {
        IntPtr buffer;
        int strLen;
        string username = "SYSTEM";
        if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WtsInfoClass.WTSUserName, out buffer, out strLen) && strLen > 1) {
            username = Marshal.PtrToStringAnsi(buffer);
            WTSFreeMemory(buffer);
            if (prependDomain) {
                if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WtsInfoClass.WTSDomainName, out buffer, out strLen) && strLen > 1) {
                    username = Marshal.PtrToStringAnsi(buffer) + "\\" + username;
                    WTSFreeMemory(buffer);
                }
            }
        }
        return username;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.