如何模拟进程以本地用户身份运行

问题描述

我正在开发一种以 localUser 身份执行进程的方法,但它以 SYstem 身份执行。我已经尝试使用下面的代码,但需要让进程执行以尝试模拟它并阻止更改它以作为用户执行此进程。

currentDir = Directory.GetCurrentDirectory();
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.SetCurrentDirectory(Path.Combine(assemblyFolder,"MyFolder\\"));

ImpersonationUtils.LaunchAsCurrentUser(currentDir);

_currentProcess = process;
process.Start();

using (ImpersonationUtils.ImpersonateCurrentUser())
{
    Logger.Log(Logger.LogSeverity.information,"Process ID: " + process.Id);
    //Process execution here.
}

这是作为currentUser执行进程的执行方法

public static void LaunchAsCurrentUser(string cmdLine)
{
    var process = Process.GetProcessesByName("myProcess").FirstOrDefault();
    if (process != null)
    {
       var token = GetPrimaryToken(process);
       if (token != IntPtr.Zero)
       {
          var envBlock = GetEnvironmentBlock(token);
          if (envBlock != IntPtr.Zero)
          {
              LaunchProcessAsUser(cmdLine,token,envBlock,process.SessionId);
              if (!DestroyEnvironmentBlock(envBlock))
              {
                  throw new Win32Exception(Marshal.GetLastWin32Error(),"DestroyEnvironmentBlock Failed");
              }
          }

          CloseHandle(token);
       }
   }
}

public static Idisposable ImpersonateCurrentUser()
{
    var process = Process.GetProcessesByName("myProcess").FirstOrDefault();
    if (process != null)
    {
        var token = GetPrimaryToken(process);
        if (token != IntPtr.Zero)
        {
            return Impersonate(token);
        }
    }

    throw new Exception("Could not find myProcess.exe");
}

有没有办法在启动进程前将执行用户从SYstem改为LocalUser?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)