title: Impersonating in C# not working for runspace (powershell)
body: <p>I have a problem with impersonating a user in C#:</p>
  
  <p>I impersonate with the known funtion used in thousands of examples:</p>
  
  <pre><code>using(Impersonator impClass = new Impersonator(_domain, _userName, _password)) ... 
  </code></pre>
  
  <p>After impersonation I run </p>
  
  <pre><code>System.Security.Principal.WindowsIdentity.GetCurrent().Name
  </code></pre>
  
  <p>and get the correct (impersonated) user. </p>
  
  <p>So impersonation is generally working. </p>
  
  <p><strong>BUT</strong> if I open a runspace (powershell) after impersonation, the old user from before impersonation is being used.</p>
  
  <p>Here's some of the code I use:</p>
  
  <pre><code>using(Impersonator impClass = new Impersonator(_domain, _userName, _password))     
  { 
      Runspace runspace = RunspaceFactory.CreateRunspace();
      runspace.Open();
  
      logger.Debug("after imp windows: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name);
  
      using (Pipeline pipeline = runspace.CreatePipeline())
      {
          List&lt;Command&gt; commandList = new List&lt;Command&gt;();
          Command cmd = new Command("whoami");
          commandList.Add(cmd);
  
          foreach (Command command in commandList)
          {
              pipeline.Commands.AddScript(command.ToString());                                                                                     pipeline.Commands.AddScript(command.ToString());
          }
  
          var res = pipeline.Invoke();
      }
  }
  </code></pre>
  
  <p>Here is what the log is saying:</p>
  
  <blockquote>
    <p>Windows Identity: "impersonatedUser" &lt;- correct</p>
    
    <p>WhoAmI From Powershell: "NotImpersonatedUser" &lt;- incorrect</p>
  </blockquote>
  
  <p>I really don't know what I am doing wrong. Please help, I've wasted so many hours already... </p>
  
  <p>This is a part of the impersonation class I am using:</p>
  
  <pre><code>  private void ImpersonateValidUser(
              string userName,
              string domain,
              string password)
          {
              WindowsIdentity tempWindowsIdentity = null;
              IntPtr token = IntPtr.Zero;
              IntPtr tokenDuplicate = IntPtr.Zero;
  
              try
              {
                  if (RevertToSelf())
                  {
                      if (LogonUser(
                          userName,
                          domain,
                          password),
                          LOGON32_LOGON_INTERACTIVE,
                          LOGON32_PROVIDER_DEFAULT,
                          ref token) != 0)
                      {
                          if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                          {
                              tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                              impersonationContext = tempWindowsIdentity.Impersonate();
  
                          }
                          else
                          {
                              throw new Win32Exception(Marshal.GetLastWin32Error());
                          }
                      }
                      else
                      {
                          throw new Win32Exception(Marshal.GetLastWin32Error());
                      }
                  }
                  else
                  {
                      throw new Win32Exception(Marshal.GetLastWin32Error());
                  }
              }
              finally
              {
                  if (token != IntPtr.Zero)
                  {
                      CloseHandle(token);
                  }
                  if (tokenDuplicate != IntPtr.Zero)
                  {
                      CloseHandle(tokenDuplicate);
                  }
              }
          }
  </code></pre>