locked
Different users are shown for whoami and "[System.Security.Principal.WindowsIdentity]::GetCurrent().Name" RRS feed

  • General discussion

  • I'm launching a PowerShell from C#. However I want to launch PowerShell and execute following commands using a different user than the logged on user. So I do this:

    • Impersonate in C#
    • Create PowerShell
    • Execute commands in PowerShell

    The user impersonation works fine in C#, I validated that. Now in order to validate in what user context is the PowerShell running, I try executing these commands which ideally should give me the impersonated user however they give different results:

    • [System.Security.Principal.WindowsIdentity]::GetCurrent().Name - returns impersonated user, indicating PowerShell is running under impersonated user.
    • whoami - gives me the logged in user, indicating the PowerShell is NOT running in impersonated user context

    There has been plenty of questions/articles on why it doesn't work in ASP.Net and the answers however I couldn't find anything for C# Windows application. I read a lot about "Pipeline Execution Thread" always using the Process's identity but there was nothing on how to force it to use thread's identity (in C# Windows application).

    Other things that I have already tried:

    • Setting the runspace's ThreadOptions to use current (C#) thread as I'm definite it is running under impersonated user.
    • Creating a PSInvocationSettings, set FlowImpersonationPolicy to true and passing it on to Powershell.Invoke.

    Any guidance or help is appreciated.

    Thanks,

    Vipin

    • Moved by jrv Tuesday, February 11, 2020 11:02 PM off-topic
    Wednesday, February 7, 2018 6:48 PM

All replies