【问题标题】:ASP.NET CORE 2.1 cross-thread impersonationASP.NET CORE 2.1 跨线程模拟
【发布时间】:2019-02-08 10:09:09
【问题描述】:

我在 ASP.NET core 2.1 中有一个非常奇怪的问题 我正在编写一个简单的 Web 应用程序,它在需要的场景中调用 Powershell 脚本。 模拟在 C# 级别上完美运行,但是一旦我调用 Powershell 脚本,它就会以默认身份(IIS Express 正在运行的身份)调用。 我知道在 ASP.NET 中很容易通过 aspnet.config 文件启用跨线程模拟,但我不知道如何在 ASP.NET Core 中执行类似的实现。 下面的代码是有问题的函数(虽然我猜函数不是问题)。

    internal void InvokeScript()
    {
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        Impersonation.LogonUser(userName, domain, password, 2, 0, out 
        SafeAccessTokenHandle handle);

        WindowsIdentity.RunImpersonated(handle, () => {

            PowerShell ps = PowerShell.Create();
            ps.Runspace = runspace;

            //Setting needed variables

            Collection<PSObject> PreFixOutput = new Collection<PSObject>();
            Collection<PSObject> FixOutput = new Collection<PSObject>();
            Collection<PSObject> PostFixOutput = new Collection<PSObject>();

            // Setting directory to needed KB subdir and finding needed stage files

            string KBtoWorkWith = RepositoryLocation + @"\" + KB.Number;

            Directory.SetCurrentDirectory(KBtoWorkWith);

            // Reading scripts from .ps1 and adding needed variables

            string commandToRun1 = string.Format("$Computer = '{0}'; $Username = '{1}'; $KB = '{2}' ; {3}", IP, Username, KB.Number, File.ReadAllText("Pre" + KB.Number + ".ps1"));
            string commandToRun2 = string.Format("$Computer = '{0}'; $Username = '{1}'; $KB = '{2}'; {3}; {4}; $success; $verbose", IP, Username, KB.Number, File.ReadAllText(KB.Number + ".ps1"), InnerFunction);
            string commandToRun3 = string.Format("$Computer = '{0}'; $Username = '{1}'; $KB = '{2}' ; {3}", IP, Username, KB.Number, File.ReadAllText("Post" + KB.Number + ".ps1"));

            // Invoking formed PS scripts & collecting returned data

            ps.AddScript(commandToRun1);

            PreFixOutput = ps.Invoke();

            OutputToUser = PreFixOutput[1].BaseObject.ToString();

            if (PreFixOutput[0].BaseObject.ToString() == "True")
            {
                IsFixApplicable = true;

                if ((PreFixOutput[2].BaseObject.ToString()) != "")
                {
                    InnerFunction = PreFixOutput[2].BaseObject.ToString();
                }

                else
                {
                    InnerFunction = "";
                }

                FixOutput = ps.AddScript(commandToRun2).Invoke();

                OutputToUser = FixOutput[1].BaseObject.ToString();

                if (FixOutput[0].BaseObject.ToString() == "True")
                {
                    PostFixOutput = ps.AddScript(commandToRun3).Invoke();

                    OutputToUser = PostFixOutput[1].BaseObject.ToString();

                    if (PostFixOutput[0].BaseObject.ToString() == "True")
                    {
                        WasFixApplied = true;
                    }

                    else
                    {
                        WasFixApplied = false;
                    }
                }

                else
                {
                    WasFixApplied = false;
                }
            }

            else
            {
                IsFixApplicable = false;
                WasFixApplied = false;
            }

            //runspace.Close();
        });

    }

【问题讨论】:

  • 如果你将Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open();移动到内部模拟会发生什么?
  • 可悲的是,然后我在打开所需的注册表错误时得到拒绝访问,同时打开运行空间,这被提到在 system.management.automation 库中非常常见。

标签: c# asp.net multithreading powershell asp.net-core


【解决方案1】:

仅供大家了解。我从来没有在 DEV 环境中解决过这个问题。在 ASP.NET CORE 和 ASP.NET 4.5.2 上都不行。这看起来是一个 IIS Express 问题。一旦我在我的机器上托管了功能齐全的 IIS 并发布了我的应用程序模拟就开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多