title: Impersonation and Activator.CreateInstance() on remote machine
body: <p>I'm using <a href="https://www.nuget.org/packages/SimpleImpersonation/" rel="nofollow">SimpleImpersonation</a> throughout my C# application to simplify the use of the Win32 LogonUser function. This works fine in all cases so far, except this particular one where I need to create a COM object instance on a remote machine with different credentials.</p>
  
  <p>Is there any way to wrap a call to <code>Activator.CreateInstance()</code> against a remote machine with alternate credentials? No matter which value for <code>LogonType</code> I pass, I get Access Denied (0x80070005).</p>
  
  <pre><code>using (Impersonation.LogonUser(domain, username, password, LogonType.Interactive))
  {
      try
      {
          Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", hostname, true);
          var comobj = Activator.CreateInstance(t); // &lt;== throws UnauthorizedAccessException
          UpdateSession session = (UpdateSession) comobj;
          IUpdateSearcher updateSearcher = session.CreateUpdateSearcher();
  
          var count = updateSearcher.GetTotalHistoryCount();
  
                  ...
  </code></pre>
  
  <p>For comparison, this works fine from Powershell if I start the prompt as a different user:</p>
  
  <pre><code>PS C:\WINDOWS\system32&gt; $updatesession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session", $hostname))
  </code></pre>