【问题标题】:C# Powershell Runspace as User, not machine accountC# Powershell Runspace 作为用户,而不是机器帐户
【发布时间】:2012-01-19 20:19:38
【问题描述】:

我有一个网站,我正试图从 powershell 运行空间运行 vbscript(听起来很奇怪,但这是我认为的最简单的方法)。我的问题是 powershell 脚本作为本地计算机帐户运行,而不是运行它的用户。任何有关解决此问题的帮助将不胜感激。我尝试使用在http://support.microsoft.com/kb/306158 找到的模拟上下文,但没有成功。提前致谢。

protected void btnRemMaint_Click(object sender, EventArgs e)
    {
        txtOutput.Text = "";
        txtOutput.Text = "Remove Maintenance Results";
        txtOutput.Text += Environment.NewLine;

        string servers;
        servers = txtServers.Text.Replace(Environment.NewLine, ",");

        while (servers[servers.Length - 1].ToString() == ",")
            servers = servers.Substring(0, servers.Length - 1);

        System.Security.Principal.WindowsImpersonationContext impersonationContext;
        impersonationContext =
            ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

        //Insert your code that runs under the security context of the authenticating user here.

        string script = "C:\\inetpub\\wwwroot\\VRS\\Admins\\PSScripts\\AMMaintMode.vbs";
        string mode = "/maint:0";
        string serv = "/server:" + servers;
        string ccserv = "/ccserver:calntmgt501";
        using (
        RunspacePool runspace = RunspaceFactory.CreateRunspacePool())
        {
            // open it
            runspace.Open();

            // create a pipeline and feed it the script text
            PowerShell pipeline = PowerShell.Create();

            pipeline.RunspacePool = runspace;
            pipeline.AddCommand("cscript");
            pipeline.AddArgument(script);
            pipeline.AddArgument(mode);
            pipeline.AddArgument(serv);
            pipeline.AddArgument(ccserv);

            // execute the script
            IAsyncResult pipeResults = pipeline.BeginInvoke();
            PSDataCollection<PSObject> pipeOutput = pipeline.EndInvoke(pipeResults);

            //pipeline.Invoke();
            for (int i = 0; i < pipeOutput.Count; i++)
            {
                txtOutput.Text += pipeOutput[i].BaseObject.ToString();
                txtOutput.Text += Environment.NewLine;
            }

            runspace.Close();
        }

        impersonationContext.Undo();

        txtServers.Text = "";
    }

输出结果如下:

Microsoft (R) Windows Script Host 版本 5.8 版权所有 (C) Microsoft 公司。保留所有权利。

AppManager 维护模式 CLI V2.1

正在连接到控制中心...服务器 calntmgt501\NQCCDB 通过 Windows 验证 * 连接服务器失败。验证凭据。错误: * 用户“BLACKROCK\USPMVVMT001$”登录失败。

【问题讨论】:

  • APP Pool 的运行方式是什么?
  • 抱歉耽搁了。 BLACKROCK\USPMVVMT001$ 是系统内置账户。
  • 应用池标识是执行代码的账户。那么为什么你说它没有在用户运行时运行呢?
  • 我需要它以访问网站的用户身份运行,而不是应用程序池。很抱歉没有说清楚。
  • 您是否通过集成的 Windows 身份验证从网站用户那里接收凭据?

标签: c# powershell


【解决方案1】:

如果网站使用 Windows 身份验证,特别是 Kerberos,则必须启用委派。这被称为“双跳”问题。更具体地说,您需要域管理员才能启用约束委派。 “受约束”部分意味着您希望将委派限制为 Web 服务器需要连接的那些服务。

否则,您需要做的就是让域管理员访问您的网站,然后您就可以使用他们的凭据做任何您想做的事情。这将是一个巨大的安全问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多