【问题标题】:Pimp my UAC and a few questions about it拉皮条我的 UAC 和一些关于它的问题
【发布时间】:2010-12-10 12:39:32
【问题描述】:

我有这个应用程序需要在受保护的路径中执行一些操作(例如 %PROGRAMFILES%),我知道我应该使用 %APPDATA%,但我现在无法更改。我已经隔离了所有可能需要 UAC 出现在另一个项目中的东西,这是一个示例代码:

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

class Class1
{
    static void Main(string[] args)
    {
        try
        {
            File.CreateText(Path.Combine(Application.StartupPath, "something.txt"));
        }
        catch (UnauthorizedAccessException ex)
        {
            MessageBox.Show(ex.Message, "UnauthorizedAccessException", MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (args.Length == 0)
            {
                Process proc = new Process();
                proc.StartInfo.FileName = Application.ExecutablePath;
                proc.StartInfo.Arguments = "not again";
                proc.StartInfo.Verb = "runas";
                proc.Start();
            }
            else
            {
                MessageBox.Show("Exit to avoid loop.");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

所以,我从我的主程序中调用这个可执行文件,如果它由于未经授权的访问而失败,它将启动自身并显示 UAC 请求。

我的问题是:

1) 我不得不将项目输出从 DLL 转换为 EXE,因为我找不到从 DLL 请求 UAC 提升的任何方法,有没有简单的方法可以做到这一点?

2) 我还注意到有些程序会显示个性化的 UAC 消息,带有程序徽标和所有这些东西,让我给你举个例子:

我怎样才能为我的程序做到这一点?

3)为了避免在以提升的权限运行时进入循环,它会得到另一个 UnauthorizedAccessException 我做了传递任何参数的事情。你会怎么做才能实现同样的目标?

我想暂时就这些了。感谢您的宝贵时间。

【问题讨论】:

标签: c# .net windows-7 windows-vista uac


【解决方案1】:

我也有同样的问题。谷歌搜索大约 2 天后,我找到了唯一适合我需求的解决方案 - 以管理权限启动应用程序。我启动应用程序,检查它是否以管理员身份运行。如果没有 - 重新启动它具有管理权限。

    static void Main(string[] args)
    {
        if (NeedElevation(args) && Elevate(args))
        { // If elevastion succeeded then quit.
            return;
        }
        // your code here
    }

    public static bool Elevate(string[] args)
    {
        try
        {
            ProcessStartInfo info = Process.GetCurrentProcess().StartInfo;
            info.Verb = "runas";
            info.Arguments = NoElevateArgument;
            foreach (string arg in args)
            {
                info.Arguments += ' ' + arg;
            }
            info.FileName = Assembly.GetEntryAssembly().Location;

            Process process = new System.Diagnostics.Process();
            process.StartInfo = info;

            process.Start();
        }
        catch (Exception)
        {
            MessageBox.Show("You don't have administrative privileges thus the Automatic Application Updates cannot be started. But the rest of application is available as usually.",
                "Not enough user rights", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }

        return true;
    }

【讨论】:

    【解决方案2】:

    1 您无法控制托管您的 DLL 的进程的提升模式。如果您可以控制安装过程,您可以为所有人during the install process grant permission to the target folder or registry

    2 你需要sign the program with a certificate published by a certificate authority that would be trusted by the client。访问您当地的证书存储(控制面板->互联网选项、内容选项卡、发布者)以查看常见的证书颁发机构。

    3 当你得到 UnauthorizedAccessExceotion 时,把它扔给宿主 exe 或返回一个错误值,表明存在安全问题。然后你的 DLL 的调用者决定做什么,比如显示一个安全错误对话框来通知用户程序是否已经被提升(权限没有被域控制器授予?),或者restarting the process in elevated mode using the runas command 如果它没有被提升。

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 2011-03-02
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多