"Access is Denied" LOGON32_LOGON_SERVICE
<p>I am trying to impersonate network service using <code>advapi32</code> </p>

<pre><code>LogonUser("NETWORK SERVICE", "NT AUTHORITY", null, LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, ref token)
</code></pre>

<p>But keep getting <code>Access is denied</code> error, on a Windows 8 machine. Any ideas? (I googled a lot but can't find an answer).</p>

<pre><code>&lt;asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;assemblyIdentity version="1.0.0.0" name="MyApplication.app" /&gt;
  &lt;trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"&gt;
    &lt;security&gt;
      &lt;requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"&gt;        
        &lt;requestedExecutionLevel level="requireAdministrator" uiAccess="false" /&gt;
      &lt;/requestedPrivileges&gt;
      &lt;applicationRequestMinimum&gt;
        &lt;defaultAssemblyRequest permissionSetReference="Custom" /&gt;
        &lt;PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site" Unrestricted="true" /&gt;
      &lt;/applicationRequestMinimum&gt;
    &lt;/security&gt;
  &lt;/trustInfo&gt;
  &lt;compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"&gt;
    &lt;application /&gt;      
  &lt;/compatibility&gt;  
&lt;/asmv1:assembly&gt;
</code></pre>

<p><strong>EDIT:</strong></p>

<p>So after few attempts the impersonation seems to work, but I cannot ever authenticate as the host's Network Service. How can an application pool run under a network service than?</p>

<p>Using </p>

<pre><code>LOGON32_LOGON_SERVICE
</code></pre>

<p>results in:</p>

<pre><code>Unhandled Exception: System.ComponentModel.Win32Exception: Access is denied
   at Tools.Network.Impersonator.Impersonate(String userName, String domainName,
 String password, LogonType logonType, LogonProvider logonProvider)
   at Tools.Network.Impersonator..ctor(String userName, String domainName, Strin
g password, LogonType logonType, LogonProvider logonProvider)
   at Impersonation.Program.Main(String[] args)
</code></pre>

<p>Using </p>

<pre><code>LOGON32_LOGON_NEW_CREDENTIALS
</code></pre>

<p>results in:</p>

<pre><code>System.IO.IOException: Logon failure: unknown user name or bad password.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Bo
olean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean ov
erwrite)
   at Impersonation.Program.Main(String[] args)
</code></pre>

<p>Puzzled.</p>
