Arion
Arion

Reputation: 41

Elevating user credentials using runasuser verb

I'm trying to elevate the process of my application using "runasuser" verb of the ProcessStartInfo class but everytime I run the program, it automatically terminates.

here is my code for the main class:

    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.
                }
            }
    }

Upvotes: 4

Views: 3749

Answers (1)

Darcy Aumiller
Darcy Aumiller

Reputation: 141

Are you sure you want "runasuser" and not "runas"? RunAs will attempt to run as an administrator, where RunAsUser will allow you to start a process as anybody.

If you really do want "runasuser", the problem seems to be that this verb will launch the username/password dialog in the same thread as the current process, but not block for a response. It also returns a null Process object in this case, so you can't query it's Respond/MainModule/... to see when it actually starts.

The only solution I've found is to enum all windows in the current process until you no longer see the username/password prompt dialog. Here's an example class; the only thing you may need/want to adjust is the trailing 500ms delay:

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();
    }
  }
}

Upvotes: 4

Related Questions