2012-09-18 147 views
0

我在ASP.NET中创建基于MVC3的Intranet应用程序。该应用程序是非常简单的:
- 要求输入(用户名)
- 管道脚本输出到HTML文件
- - 重定向到创建的HTML文件中使用提供的用户名(获取从远程数据库中的一些数据)
运行PowerShell脚本。

出于安全原因,我使用Windows身份验证,仅对特定用户组进行访问。这对于应用程序本身非常有用 - 我可以在角落看到当前的用户名,而不是指定的用户无权访问。

问题是PS脚本本身。它似乎没有与用户的凭据一起工作,所以我将whoami行添加到脚本并得到结果:iis apppool\asp.net v4.0

这是我用来运行PS脚本代码:
ASP.NET - 使用传递身份验证(Windows身份验证)运行PowerShell脚本

var ps = PowerShell.Create(); 
ps.Commands.AddScript(@"CommandHere"); 
ps.Invoke(); 

我如何使用当前用户的凭据运行它?

+0

为什么不直接通过Windows进行脚本授权。它将使用Windows凭据。在你的asp.net表单中添加验证,只允许任何人访问。 http://blogs.msdn.com/b/dvespa/archive/2010/02/22/how-to-use-windows-authentication-with-the-pscredential-class.aspx – Botonomous

+0

我很安静PowerShell和ASP.NET新手。你能给我更多的细节如何在我的代码中使用它吗? –

回答

0

我有我的内网同样的问题

我尝试: 的web.config

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

在我的控制器

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

我有我的网页上相同的结果:

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