gpt4 book ai didi

c#-4.0 - 使用 runasuser 动词提升用户凭据

转载 作者:行者123 更新时间:2023-12-02 02:27:11 26 4
gpt4 key购买 nike

我正在尝试使用 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.
}
}
}

最佳答案

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

如果你真的想要“runasuser”,问题似乎是这个动词将在与当前进程相同的线程中启动用户名/密码对话框,但不会阻塞响应。在这种情况下,它还会返回一个 null 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();
}
}
}

关于c#-4.0 - 使用 runasuser 动词提升用户凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5483439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com