C# 用户模拟未传递给子进程

C# User Impersonation not being passed on to sub-processes

我正在尝试从控制台应用程序为所有进程 运行 模拟用户。我不想以该用户身份启动我的 VisualStudio,也不想在该用户下继续附加外部进程 运行。

这是我试过的...

       static void Main(string[] args)
        {
            Console.WriteLine("Environment.UserName: {0}", Environment.UserName); // Prints currently logged-in win username

            ConfigurationManager.GetSection("configuration");                    

            using (var impersonator = new Impersonator())
            {
                Console.WriteLine("Environment.UserName: {0}", Environment.UserName); // Prints impersonated username

                var hostAccount = new System.ServiceModel.ServiceHost(typeof(AccountService));  // WCF Service class            
                hostAccount.Open();
            }
        }

如果我尝试在 AccountService class 的任何方法中检查 Environment.UserName 的值,它总是给我当前登录的 win 用户名而不是模拟用户。

My Impersonator class 使用以下代码来模拟用户。

bool returnValue = LogonUser(user, userDomain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref _userHandle);

if (!returnValue)
    throw new ApplicationException("Could not impersonate user");

WindowsIdentity newId = new WindowsIdentity(_userHandle);
_impersonatedUser = newId.Impersonate();

我正在尝试实现以下代码所做的事情,但 运行 应用程序处于调试模式并且不更改任何当前 wcf 配置。

Process.Start("path to exe", "user", ssPwd, "MyDOMAIN");

我明白 运行 作为用户的整个应用程序与在应用程序内模拟用户之间存在差异。但只是想看看是否有人遇到类似问题并设法找到解决方案。

编辑 1:

我在模拟用户后尝试启动新线程,但它们都在模拟用户下 运行。所以新线程确实获得了模拟上下文。我想这与 wcf host 的启动方式有关。

wcfhost.open() 始终在主用户身份下运行,而不是在模拟身份下运行。所以现在我在另一个控制台应用程序的不同用户下启动我的主控制台应用程序并将调试器附加到它并且它可以工作。

如果对任何人有帮助,我已将我的代码复制到下面。

using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using EnvDTE80;
using Process = System.Diagnostics.Process;

namespace StartService
{
    class Program
    {
        static void Main(string[] args)
        {
            var secure = new SecureString();
            foreach (var c in "password-from-config")
            {
                secure.AppendChar(c);
            }

            Process process = null;

            try
            {
                process = Process.Start(@"C:\Test Projects\WcfServiceTest\WcfServiceTest\bin\Debug\WcfServiceTest.exe",
                    "TestUser", secure, "DomainName");

                Attach(GetCurrent());

                Console.ReadKey();
            }
            finally
            {
                if (process != null && !process.HasExited)
                {
                    process.CloseMainWindow();
                    process.Close();
                }    
            }
        }

        public static void Attach(DTE2 dte)
        {
            var processes = dte.Debugger.LocalProcesses;
            foreach (var proc in processes.Cast<EnvDTE.Process>().Where(proc => proc.Name.IndexOf("WcfServiceTest.exe") != -1))
                proc.Attach();
        }

        internal static DTE2 GetCurrent()
        {
            var dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0"); // Specific to VS2013

            return dte2;
        }
    }
}