title: Could not access Remote Windows Services
body: <p>I have a website where users can Impersonate/Authenticate themselves to a remote server, and control specific windows services. It <strong>works fine</strong> if I am running the project <strong>locally</strong>, but when I <strong>publish</strong> the project onto the <strong>Web Server</strong> having <em>IIS server</em> running, It gives an exception:</p>
  
  <p><em>Cannot open Service Control Manager on computer (computer name). This operation might require other privileges.</em></p>
  
  <p><strong>My Work:</strong></p>
  
  <ol>
  <li><p>A class that impersonates(works good locally):</p>
  
  <pre><code>public class ImpersonationUtil
  {
      public static bool Impersonate()
      {
          string logon = "ADMIN";
          string password = "PASS";
          string domain = "THEDOMAIN";
          IntPtr token = IntPtr.Zero;
          IntPtr tokenDuplicate = IntPtr.Zero;
          WindowsImpersonationContext impersonationContext = null;
  
          if (LogonUser(logon, domain, password, 2, 0, ref token) != 0)
              if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                  impersonationContext = new WindowsIdentity(tokenDuplicate).Impersonate();
  
          return (impersonationContext != null);
      }
  
      [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
      public static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  
      [DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
      public extern static int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
  }
  </code></pre></li>
  <li><p>Code to connect with services:</p>
  
  <pre><code>    ServiceController controller = new ServiceController("serviceName", "machineAddress");
      var status = controller.Status;
  </code></pre></li>
  <li><p>Web.config stuff(To read and accept the impersonation on web server):</p>
  
  <p><code>&lt;system.webServer&gt;</code>
      <code>&lt;validation validateIntegratedModeConfiguration="false"/&gt;</code>
  <code>&lt;/system.webServer&gt;</code></p></li>
  </ol>
  
  <p>Any Help?</p>