使用runasuser动词提升用户凭据

时间:2011-03-30 07:50:08

标签: c#-4.0

我正在尝试使用ProcessStartInfo类的“runasuser”动词提升我的应用程序的进程,但每次运行程序时,它都会自动终止。

这是主要类的代码:

    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Application Events
        Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

        //Check if the current user is a member of the administrator group
        WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        bool hasAdministrativeRights = principal.IsInRole(WindowsBuiltInRole.Administrator);

        bool createdNew = false;
        if (hasAdministrativeRights)
            //Creating new mutex for single instance
            using (Mutex mutex = new Mutex(true, "CpELabAppCopier", out createdNew))
            {
                if (createdNew)
                    Application.Run(new MainForm());
                else
                    Application.Exit();
            }
        else
            //Creating new mutex for single instance
            using (Mutex mutex = new Mutex(true, "Elevated_CpELabAppCopier", out createdNew))
            {
                if (createdNew)
                {
                    //Setting the startinfo
                    ProcessStartInfo newProcessInfo = new ProcessStartInfo();
                    newProcessInfo.FileName = Application.ExecutablePath;
                    newProcessInfo.Verb = "runasuser";
                    newProcessInfo.UseShellExecute = true;

                    //Starting new process
                    Process newProcess = new Process();
                    newProcess.StartInfo = newProcessInfo;
                    newProcess.Start();

                    //The Run As dialog box will show and close immediately.
                }
            }
    }

1 个答案:

答案 0 :(得分:2)

你确定要“runasuser”而不是“runas”吗? RunAs将尝试以管理员身份运行,RunAsUser允许您以任何人身份启动流程。

如果你确实想要“runasuser”,问题似乎是这个动词将在与当前进程相同的线程中启动用户名/密码对话框,但不会阻止响应。在这种情况下,它还返回一个空的Process对象,因此您无法查询它的Respond / MainModule / ...以查看它实际启动的时间。

我找到的唯一解决方案是在当前进程中枚举所有窗口,直到您不再看到用户名/密码提示对话框。这是一个示例类;您可能需要/想要调整的唯一事情是尾随500毫秒延迟:

using System;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace TestAsUser
{
  public static class testClass
  {
    public delegate bool EnumThreadDelegate (IntPtr hwnd, IntPtr lParam);
    [DllImport("user32.dll")] static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam);
    [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
    [DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hwnd);

    private static bool containsSecurityWindow;

    public static void Main(string[] args)
    {
      ProcessStartInfo psi = new ProcessStartInfo("c:\\windows\\system32\\notepad.exe");
      psi.Verb = "runasuser";
      Process.Start(psi);

      containsSecurityWindow = false;
      while(!containsSecurityWindow) // wait for windows to bring up the "Windows Security" dialog
      {
        CheckSecurityWindow();
        Thread.Sleep(25);
      }
      while(containsSecurityWindow) // while this process contains a "Windows Security" dialog, stay open
      {
        containsSecurityWindow = false;
        CheckSecurityWindow();
        Thread.Sleep(50);
      }
      Thread.Sleep(500);            // give some time for windows to complete launching the application after closing the dialog
    }

    private static void CheckSecurityWindow()
    {
      ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads;
      for(int i=0; i<ptc.Count; i++)
        EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero);
    }

    private static bool CheckSecurityThread(IntPtr hwnd, IntPtr lParam)
    {
      if(GetWindowTitle(hwnd) == "Windows Security")
        containsSecurityWindow = true;
      return true;
    }

    private static string GetWindowTitle(IntPtr hwnd)
    {
      StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1);
      GetWindowText(hwnd, sb, sb.Capacity);
      return sb.ToString();
    }
  }
}