使用runasuser verb升级用户凭据 [英] Elevating user credentials using runasuser verb
问题描述
我尝试使用ProcessStartInfo类的runasuser动词来提升我的应用程序的进程,但是每次运行该程序时,它都会自动终止。
是我的代码主类:
private static void Main()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault(false);
//应用程序事件
Application.ThreadException + = new ThreadExceptionEventHandler(Application_ThreadException);
//检查当前用户是否是管理员组的成员
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRights = principal.IsInRole(WindowsBuiltInRole.Administrator);
bool createdNew = false;
if(hasAdministrativeRights)
//为单实例创建新的互斥量
使用(Mutex mutex = new Mutex(true,CpELabAppCopier,out createdNew))
{
if(createdNew)
Application.Run(new MainForm());
else
Application.Exit();
}
else
//为单实例创建新的互斥量
使用(Mutex mutex = new Mutex(true,Elevated_CpELabAppCopier,out createdNew))
{
if(createdNew)
{
//设置startinfo
ProcessStartInfo newProcessInfo =新的ProcessStartInfo();
newProcessInfo.FileName = Application.ExecutablePath;
newProcessInfo.Verb =runasuser;
newProcessInfo.UseShellExecute = true;
//启动新进程
Process newProcess = new Process();
newProcess.StartInfo = newProcessInfo;
newProcess.Start();
//运行方式对话框将立即显示和关闭。
}
}
}
您确定要使用runasuser而不是runas吗?
RunAs将尝试以管理员身份运行,其中RunAsUser将允许您作为任何人启动一个进程。
如果真的需要runasuser问题似乎是,这个动词将在与当前进程相同的线程中启动用户名/密码对话框,但不阻止响应。在这种情况下,它还返回一个空的Process对象,所以你不能查询它的Respond / MainModule / ...看看它实际上什么时候开始。
我发现是在当前进程中枚举所有窗口,直到你不再看到用户名/密码提示对话框。这里是一个示例类;你可能需要/想要调整的唯一的事情是拖尾500毫秒的延迟:
using System;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
命名空间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)//等待窗口打开Windows安全对话框
{
CheckSecurityWindow();
Thread.Sleep(25);
}
while(containsSecurityWindow)//此过程包含一个Windows安全对话框,保持打开
{
containsSecurityWindow = false;
CheckSecurityWindow();
Thread.Sleep(50);
}
Thread.Sleep(500); //给一些时间窗口在关闭对话框之后完成启动应用程序
}
private static void CheckSecurityWindow()
{
ProcessThreadCollection ptc = Process.GetCurrentProcess ().Threads;
for(int i = 0; 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;
}
私人静态字符串GetWindowTitle(IntPtr hwnd)
{
StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd)+ 1);
GetWindowText(hwnd,sb,sb.Capacity);
return sb.ToString();
}
}
}
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.
}
}
}
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();
}
}
}
这篇关于使用runasuser verb升级用户凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!