0

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Alban
  • 3,105
  • 5
  • 31
  • 46
  • Are you creating the powershell object ( `PSRunner ?` ) under the WindowsImpersonationContext ? – CB. Mar 29 '13 at 12:04
  • Yes i have created an object : PSRunner.ExecuteScript(), to run my powershell command and read and return the output result. – Alban Apr 01 '13 at 19:26

1 Answers1

1

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

Tomas Panik
  • 4,337
  • 2
  • 22
  • 31