在ASP.net intranet中以windows登录用户运行powershell

本文关键字:登录 用户 运行 powershell windows 中以 ASP net intranet | 更新日期: 2023-09-27 18:06:31

我在ASP中创建MVC intranet应用程序。. NET c#与Windows身份验证,我需要运行一个外部powershell脚本与用户的凭据。

我添加了我的网页。配置文件。

<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();

但是我总是在powershell中使用IIS用户

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

在ASP.net intranet中以windows登录用户运行powershell

我不知道PSRunner如何初始化powershell运行时,但是如何启动powershell进程作为其他用户使用:Runas