title: Network Authentication when running exe from WMI
body: <p>I have a C# exe that needs to be run using WMI and access a network share. However, when I access the share I get an UnauthorizedAccessException. If I run the exe directly the share is accessible. I am using the same user account in both cases.</p>
  
  <p>There are two parts to my application, a GUI client that runs on a local PC and a backend process that runs on a remote PC. When the client needs to connect to the backend it first launches the remote process using WMI (code reproduced below). The remote process does a number of things including accessing a network share using Directory.GetDirectories() and reports back to the client.</p>
  
  <p>When the remote process is launched automatically by the client using WMI, it cannot access the network share. However, if I connect to the remote machine using Remote Desktop and manually launch the backend process, access to the network share succeeds.</p>
  
  <p>The user specifed in the WMI call and the user logged in for the Remote Desktop session are the same, so the permissions should be the same, shouldn't they?</p>
  
  <p>I see in the MSDN entry for <a href="http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx" rel="noreferrer">Directory.Exists()</a> it states "The Exists method does not perform network authentication. If you query an existing network share without being pre-authenticated, the Exists method will return false." I assume this is related? How can I ensure the user is authenticated correctly in a WMI session?</p>
  
  <pre><code>ConnectionOptions opts = new ConnectionOptions();
  
  opts.Username = username;
  opts.Password = password;
  
  ManagementPath path = new ManagementPath(string.Format("\\\\{0}\\root\\cimv2:Win32_Process", remoteHost));
  
  ManagementScope scope = new ManagementScope(path, opts);
  
  scope.Connect();
  
  ObjectGetOptions getOpts = new ObjectGetOptions();
  using (ManagementClass mngClass = new ManagementClass(scope, path, getOpts))
  {
      ManagementBaseObject inParams = mngClass.GetMethodParameters("Create");
      inParams["CommandLine"] = commandLine;
      ManagementBaseObject outParams = mngClass.InvokeMethod("Create", inParams, null);
  }
  </code></pre>