Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

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


    CC Regular

  • Member
  • PipPipPip
  • 44 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 (  The application runs fine on 2012 ( 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 (  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;

        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);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download