Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Terminal services and Impersonation

impersonation c# terminal server

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 Tchpowdog

Tchpowdog

    CC Regular

  • Member
  • PipPipPip
  • 43 posts

Posted 11 September 2015 - 07:15 AM

I wrote an application that runs on Terminal Server (or RDS now).  We just upgraded our servers from Windows Server 2003 to Windows Server 2012.  My application ran flawlessly on 2003 (10.0.2.3).  The application runs fine on 2012 (10.0.2.1) except for where I use impersonation in my application.  I have an admin section in the software that allows admins to restart Windows Services on a different server (10.0.2.2).  The error I get is not specific, it says "exception (0xe0434352)".  I traced that back to the kernel32.dll file I import for the impersonation.  It seems as though my application cannot import the file from the new terminal server.  If I log onto the terminal server as an administrator, everything works fine.  These are the files I import for impersonation:

 

       
 public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;


        WindowsImpersonationContext impersonationContext;


        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);


        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();


        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);