Alban
Alban

Reputation: 3215

Run powershell as logged windows user in ASP.net intranet

I'm creating MVC intranet application in ASP.NET C# with Windows authentification , and I need run an external powershell script with users credential.

I add in my web.config file.

<system.web>
    <authentication mode="Windows" />
    <identity impersonate="true"/>
</system.web>
<runtime>
    <!--http://blogs.msdn.com/b/powershell/archive/2007/09/10/impersonation-and-hosting-powershell.aspx-->
    <legacyImpersonationPolicy enabled="false"/>
    <alwaysFlowImpersonationPolicy enabled="true"/>
</runtime>

in my controller

        // http://support.microsoft.com/kb/306158
        System.Security.Principal.WindowsImpersonationContext impersonationContext;
        impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

        ViewBag.Message = "User Powershell : " + PSRunner.ExecuteScript("Whoami;") + "   > user C# : " + System.Security.Principal.WindowsIdentity.GetCurrent().Name + "    User Authentifier : " + User.Identity.Name + " type " + User.Identity.GetType();

        impersonationContext.Undo();

But i have always IIS user in powershell

User Powershell : iis apppool\www.site.com > user C# : DOMAIN\alopez User Authentifier : DOMAIN\alopez type System.Security.Principal.WindowsIdentity

Upvotes: 0

Views: 1505

Answers (1)

Tomas Panik
Tomas Panik

Reputation: 4609

I don't know how PSRunner initializes powershell runtime, but what about starting powershell process as other user using: Runas

Upvotes: 1

Related Questions