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

时间:2016-02-02 15:22:52

标签: c# .net powershell impersonation runspace

我在使用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();
    }
}

以下是日志的内容:

  

Windows Identity:&#34; impersonatedUser&#34; &lt; - 正确

     

来自Powershell的WhoAmI:&#34; NotImpersonatedUser&#34; &lt; - 不正确

我真的不知道自己做错了什么。请帮忙,我已经浪费了这么多时间......

这是我正在使用的模拟类的一部分:

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

0 个答案:

没有答案