如何提升.net应用程序权限?

发布于 2024-12-03 18:30:20 字数 504 浏览 0 评论 0原文

我有一个应用程序,它会在启动时检查更新,如果发现更新,它会通过网络将一些文件复制到程序文件文件夹。显然,在正常情况下,标准用户无法执行此类任务。

我尝试创建一个服务来执行更新过程,但遇到了一些安全问题,并且我 在超级用户中询问了这个问题

现在,考虑到大多数应用程序需要提升权限才能执行此类任务,我认为这可能是正确的方法。但是,如何在自 XP(包括 XP)的所有 Windows 版本下请求更新程序的提升。我发现了许多关于清单文件的主题,但由于我需要它与 XP 一起使用,所以我无法专门为 UAC

I have an application that would check for updates upon start and, if updates are found, it would copy some files over the network to the program files folder. Obviously such task can't be performed by Standard Users under normal scenarios.

I tried creating a service to do the update process but I had some security issues and I asked this question about it in superusers.

Now, considering the fact that most applications require elevated privileges to perform such task I think that might be the right approach. But how do I request elevation for the updater under all Windows version as of XP, included. I've found many topics about a manifest file, but since I need this to work with XP I can't create a solution specifically for UAC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-12-10 18:30:20

进程的权限只能在启动时提升;正在运行的进程的权限无法提升。为了提升现有应用程序的权限,必须创建应用程序进程的新实例,其动词为“runas”:

private static string ElevatedExecute(NameValueCollection parameters)
{
    string tempFile = Path.GetTempFileName();
    File.WriteAllText(tempFile, ConstructQueryString(parameters));

    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.UseShellExecute = true;
        startInfo.WorkingDirectory = Environment.CurrentDirectory;
        Uri uri = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);
        startInfo.FileName = uri.LocalPath;
        startInfo.Arguments = "\"" + tempFile + "\"";
        startInfo.Verb = "runas";
        Process p = Process.Start(startInfo);
        p.WaitForExit();
        return File.ReadAllText(tempFile);
    }
    catch (Win32Exception exception)
    {
        return exception.Message;
    }
    finally
    {
        File.Delete(tempFile);
    }
}

用户以管理员身份确认执行程序后,将执行同一应用程序的另一个实例,而无需 UI;一个可以显示一个在没有提升权限的情况下运行的 UI,另一个可以显示在后台以提升的权限运行的 UI。第一个进程等待,直到第二个进程完成执行。有关详细信息和工作示例,您可以查看 MSDN 存档

为了防止在某个漫长的过程中发生所有这些对话框恶作剧,您需要通过 在应用程序中嵌入适当的清单以要求“highestAvailable”执行级别:这将导致应用程序启动后立即出现 UAC 提示,并且导致所有子进程以提升的权限运行,而无需额外提示。

Privileges can only be elevated at startup for a process; a running process' privileges cannot be elevated. In order to elevate an existing application, a new instance of the application process must be created, with the verb “runas”:

private static string ElevatedExecute(NameValueCollection parameters)
{
    string tempFile = Path.GetTempFileName();
    File.WriteAllText(tempFile, ConstructQueryString(parameters));

    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.UseShellExecute = true;
        startInfo.WorkingDirectory = Environment.CurrentDirectory;
        Uri uri = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);
        startInfo.FileName = uri.LocalPath;
        startInfo.Arguments = "\"" + tempFile + "\"";
        startInfo.Verb = "runas";
        Process p = Process.Start(startInfo);
        p.WaitForExit();
        return File.ReadAllText(tempFile);
    }
    catch (Win32Exception exception)
    {
        return exception.Message;
    }
    finally
    {
        File.Delete(tempFile);
    }
}

After the user confirms the execution of the program as administrator, another instance of the same application is executed without a UI; one can display a UI running without elevated privileges, and another one running in the background with elevated privileges. The first process waits until the second finishes its execution. For more information and a working example you can check out the MSDN archive.

To prevent all this dialog shenanigans in the middle of some lengthy process, you'll need to run your entire host process with elevated permissions by embedding the appropriate manifest in your application to require the 'highestAvailable' execution level: this will cause the UAC prompt to appear as soon as your app is started, and cause all child processes to run with elevated permissions without additional prompting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文