0

I'm trying to write an application that runs the Draytek VPN client as another user as I don't want the user to have an admin password and their account is a standard user. However I can't get it working due to errors in both implementations I've tried.

Here is some sample code I've tried:

System.Security.SecureString ss = new System.Security.SecureString();
foreach (var c in "password")
{
    ss.AppendChar(c);
}

Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.WorkingDirectory = "c:\\";
process.StartInfo.FileName = @"C:\Program Files (x86)\DrayTek\Smart VPN Client\SmartVPNClient.exe";
process.StartInfo.Verb = "runas";
process.StartInfo.UserName = "testUser";
process.StartInfo.Password = ss;
process.StartInfo.UseShellExecute = false;
process.Start();

I get the error: The requested operation requires elevation.

I have also tried Impersonation using the code in the question, Impersonating a Windows user. My implementation of the call is below:

  using ( new Impersonator( "testUser", "", "password" ) )
  {

      Process process = new Process();
      process.StartInfo.UseShellExecute = true;
      process.StartInfo.WorkingDirectory = "c:\\";
      process.StartInfo.FileName = @"C:\Program Files (x86)\DrayTek\Smart VPN Client\SmartVPNClient.exe";
      process.StartInfo.Verb = "runas";
      process.Start();
  }

However the error from running the above code is Unknown error (0xfffffffe).

I can provide more error details if required.

4

1 回答 1

0

关于第一种方法:

您收到“请求的操作需要提升”消息的事实意味着当前运行您的应用程序的用户没有以管理员身份运行,或者 Draytek VPN 客户端需要提升才能正常运行。

我对您的代码进行了以下更改:

  1. 删除了process.StartInfo.Verb = "runas";不需要的行。
  2. 将文件名更改为测试控制台应用程序,而不是 Draytek VPN 客户端)。

并测试如下:

Windows 7的

  • 以管理员身份登录并在默认级别启用 UAC
  • 以受限用户身份登录,并在默认级别启用 UAC

视窗

  • 以管理员身份登录
  • 以受限用户身份登录

所有测试都有效。在任务管理器中,我可以看到测试控制台在正确的帐户下运行。

这是我用来运行测试的代码:

static void Main(string[] args)
        {
            Console.Write("User name:");
            string user = Console.ReadLine();
            Console.Write("Password:");
            string password = Console.ReadLine();
            Console.Write("File name:");
            string fileName = Console.ReadLine();

            System.Security.SecureString ss = new System.Security.SecureString();
            foreach (var c in password)
            {
                ss.AppendChar(c);
            }

            Process process = new Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.WorkingDirectory = "c:\\";
            process.StartInfo.FileName = fileName;
            process.StartInfo.Verb = "runas";
            process.StartInfo.UserName = user;
            process.StartInfo.Password = ss;
            process.StartInfo.UseShellExecute = false;
            process.Start();

            Console.ReadKey();
        }

您是否尝试过运行另一个应用程序而不是 Draytek VPN Client 作为测试?

于 2012-11-23T00:59:20.410 回答