Unable to get the Current User's Token information

Posted by Ram on Stack Overflow See other posts from Stack Overflow or by Ram
Published on 2010-05-21T11:21:27Z Indexed on 2010/05/21 11:40 UTC
Read the original article Hit count: 541

Filed under:

Hi, I have been trying to get the currently logged-in user's token information using the following code :

    [DllImport("wtsapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool WTSQueryUserToken(int sessionId, out IntPtr tokenHandle);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern uint WTSGetActiveConsoleSessionId();

    static void Main(string[] args)
    {
        try
        {
            int sessionID = (int)WTSGetActiveConsoleSessionId();
            if (sessionID != -1)
            {

                System.IntPtr currentToken = IntPtr.Zero;

                bool bRet = WTSQueryUserToken(sessionID, out currentToken);
                Console.WriteLine("bRet : " + bRet.ToString());
            }
        }
        catch (Exception)
        {
        }
    }

The problem is that, bRet is always false and currentToken is always 0. I am getting the sessionid as 1.

Could someone tell me what's going wrong here?

I want to use this token information to pass it to the CreateProcessAsUser function from a windows service.

Thanks, Ram

© Stack Overflow or respective owner

Related posts about c#