3

Design Flow: Non-Admin User ----> Program A (Non-Admin) --> Program B (Requires Admin)

I'm writing a program to launch a program that requires admin rights from a non-admin account. The program is launched on start-up with any user and it launches the second program (requiring admin rights) as a different user who has admin rights. The issue I am having is the program is saying that to launch a program as a different user it requires admin rights. I know this not to be true so I know I have something incorrect in my code to launch the second process.

The code is as follows:

try
{
    ProcessStartInfo myProcess = new ProcessStartInfo(path);
    myProcess.UserName = username;
    myProcess.Password = MakeSecureString(password);
    myProcess.WorkingDirectory = @"C:\Windows\System32";
    myProcess.UseShellExecute = false;
    myProcess.Verb = "runas"; //Does not work with or without this command
    Process.Start(myProcess);
}

The exception is as follows:

System.ComponentModel.Win32Exception (0x80004005): The requested operation requires elevation
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Loader.Program.RunProgram(String path, String username, String password)
4

1 回答 1

1

我猜你指的是在新创建的进程上触发 UAC 提示。如果是这样,只需几行就可以解决问题,特别是删除用户/密码属性(因为它们会被 Windows 询问)并设置UseShellExecutetrue

ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.WorkingDirectory = @"C:\Windows\System32";
myProcess.UseShellExecute = true;
myProcess.Verb = "runas";
Process.Start(myProcess);

我注意到这种方法的两个警告是,如果用户取消提示,则会抛出异常,说明用户已取消(您必须准备好取消处理)。此外,如果 UAC 在系统上被禁用/不存在,则不会提升该进程。为了解决这个问题,启动的程序必须准备好检查它是否真的被授予管理员权限。

一种完全不同的方法是将清单添加到目标应用程序(如果您能够重新编译它),指定它需要管理员权限。

于 2013-08-01T02:25:23.553 回答