在C#中模拟不适用于运行空间(powershell)

在C#中模拟不适用于运行空间(powershell),c#,.net,powershell,impersonation,runspace,C#,.net,Powershell,Impersonation,Runspace,我在C#中模拟用户时遇到问题: 我使用数千个示例中使用的已知功能进行模拟: using(Impersonator impClass = new Impersonator(_domain, _userName, _password)) ... 模仿之后我跑了 System.Security.Principal.WindowsIdentity.GetCurrent().Name 并获取正确的(模拟的)用户 因此,模仿通常是有效的 但是如果在模拟之后打开运行空间(powershell),则使用模

我在C#中模拟用户时遇到问题:

我使用数千个示例中使用的已知功能进行模拟:

using(Impersonator impClass = new Impersonator(_domain, _userName, _password)) ... 
模仿之后我跑了

System.Security.Principal.WindowsIdentity.GetCurrent().Name
并获取正确的(模拟的)用户

因此,模仿通常是有效的

但是如果在模拟之后打开运行空间(powershell),则使用模拟之前的旧用户

以下是我使用的一些代码:

using(Impersonator impClass = new Impersonator(_domain, _userName, _password))     
{ 
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();

    logger.Debug("after imp windows: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name);

    using (Pipeline pipeline = runspace.CreatePipeline())
    {
        List<Command> commandList = new List<Command>();
        Command cmd = new Command("whoami");
        commandList.Add(cmd);

        foreach (Command command in commandList)
        {
            pipeline.Commands.AddScript(command.ToString());                                                                                     pipeline.Commands.AddScript(command.ToString());
        }

        var res = pipeline.Invoke();
    }
}

什么是
模拟者
类?这是您自己的模拟实现吗?不,我使用了以下示例:在事件日志中,它看起来很好:分配给新登录的特权。我还为另一个项目(文件系统上的权限)使用了相同的模拟功能,它工作得很好,所以我认为模拟本身工作得很好。我在问题中添加了模拟函数的私有函数。你有没有找到解决这个问题的有效方法?
  private void ImpersonateValidUser(
            string userName,
            string domain,
            string password)
        {
            WindowsIdentity tempWindowsIdentity = null;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            try
            {
                if (RevertToSelf())
                {
                    if (LogonUser(
                        userName,
                        domain,
                        password),
                        LOGON32_LOGON_INTERACTIVE,
                        LOGON32_PROVIDER_DEFAULT,
                        ref token) != 0)
                    {
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                            tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                            impersonationContext = tempWindowsIdentity.Impersonate();

                        }
                        else
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error());
                        }
                    }
                    else
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (token != IntPtr.Zero)
                {
                    CloseHandle(token);
                }
                if (tokenDuplicate != IntPtr.Zero)
                {
                    CloseHandle(tokenDuplicate);
                }
            }
        }