5

I have developed an ASP.NET MVC Web Application to execute PowerShell scripts.

I am using the VS web server and can execute scripts fine.

However, a requirement is that users are able to execute scripts against AD to perform actions that their own user accounts are not allowed to do.

Therefore I am using impersonation to switch the identity before creating the PowerShell runspace:

            Runspace runspace = RunspaceFactory.CreateRunspace(config);

        var currentuser = WindowsIdentity.GetCurrent().Name;

        if (runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen) {
            runspace.Open();
        }

I have tested using a domain admin account and I get the following exception when calling runspace.Open():

Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. Exception Details: System.Security.SecurityException: Requested registry access is not allowed.

The web application is running in full trust and I have explicitly added the account I am using for impersonation to the local administrators group of the machine (even though the domain admins group was already there).

I'm using advapi32.dll LogonUser call to perform the impersonation in a similar way to this post (http://blogs.msdn.com/webdav_101/archive/2008/09/25/howto-calling-exchange-powershell-from-an-impersonated-thead.aspx)

Any help appreciated as this is a bit of a show stopper at the moment.

Thanks Ben

1 Answer 1

3

Does this blog post help? Comes straight from the PowerShell devs. Essentially, PowerShell starts a new thread to run the pipeline, and since .NET2.0 doesn't allow the thread to use the impersonation from the calling thread, it fails.

http://blogs.msdn.com/powershell/archive/2007/09/10/impersonation-and-hosting-powershell.aspx

2
  • Hi James, I did come across this post when originally and it didn't work for me. However, after trying today on another machine (and making sure I did a reboot after changing the aspnet.config file - important!) I was able to impersonate another user successfully. Thanks
    – Ben Foster
    May 4, 2010 at 10:13
  • yet another broken Microsoft support link... blogs.msdn.com/powershell/archive/2007/09/10/… Mar 30, 2021 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.