控制台应用程序需要UAC

发布于 2024-07-08 06:29:12 字数 699 浏览 6 评论 0原文

我有一个控制台应用程序,需要使用一些需要管理员级别的代码。 我读到我需要添加一个清单文件 myprogram.exe.manifest ,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator">
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

但它仍然不会引发 UAC(在控制台中或在 VS 中调试)。 我该如何解决这个问题?

更新

如果我在管理员中运行解决方案或在管理员中运行 /bin/*.exe ,我就可以使其工作。 我仍然想知道是否有可能在应用程序启动时弹出一些内容,而不是明确右键单击>以管理员身份运行?

I have a console application that require to use some code that need administrator level. I have read that I need to add a Manifest file myprogram.exe.manifest that look like that :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator">
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

But it still doesn't raise the UAC (in the console or in debugging in VS). How can I solve this issue?

Update

I am able to make it work if I run the solution in Administrator or when I run the /bin/*.exe in Administrator. I am still wondering if it's possible to have something that will pop when the application start instead of explicitly right click>Run as Administrator?

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

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

发布评论

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

评论(3

只为守护你 2024-07-15 06:29:12

对于任何使用 Visual Studio 的人来说,这都非常简单。 我正要设置 Windows SDK 并执行 mt.exe 构建后步骤以及所有这些,然后才意识到它已内置到 VS 中。 我想我应该把它记录下来供后代使用。

  1. 项目| 添加新项目 -> Visual C# 项目 -> 应用程序清单文件
  2. 打开app.manifest,将requestedExecutionLevel.@level更改为“requireAdministrator”
  3. 构建

Ta-da

For anyone using Visual Studio, it's super easy. I was about to go set up the Windows SDK and do mt.exe post-build steps and all that before realizing it's built into VS. I figured I'd record it for posterity.

  1. Project | Add New Item -> Visual C# Items -> Application Manifest File
  2. Open app.manifest, change requestedExecutionLevel.@level to "requireAdministrator"
  3. Build

Ta-da

流年已逝 2024-07-15 06:29:12

斯科特的回答将按照您的要求进行操作,但 Microsoft 建议控制台应用程序显示“访问被拒绝”消息,而不是提示提升权限。

来自http://msdn.microsoft.com/en-us/library/bb756922。 ASPX

控制台应用程序在控制台窗口上显示其输出,而不是
具有单独的用户界面。 如果应用程序需要完整的管理员
要运行的访问令牌,然后需要启动该应用程序
一个升高的控制台窗口。

您必须对控制台应用程序执行以下操作:

  1. 将您的应用程序标记为“asInvoker”:您可以通过编写应用程序清单(在其中设置 RequestedExecutionLevel == asInvoker)来完成此操作。 此设置允许来自非提升上下文的调用者创建您的流程,从而允许他们继续执行步骤 2。

  2. 如果应用程序在没有完整管理员访问令牌的情况下运行,则提供错误消息:如果应用程序在非提升控制台中启动,您的应用程序应给出简短消息并退出。 建议的消息是:“访问被拒绝。需要管理员权限才能使用所选选项。使用管理员命令提示符完成这些任务。”

应用程序还应在启动失败时返回错误代码 ERROR_ELEVATION_REQUIRED,以方便编写脚本。

我的 C# 代码如下。 它在 Windows XP(管理员 -> 确定,标准用户 -> 拒绝)和 Windows Server 2008(提升的管理员 -> 确定,非提升的管理员 -> 拒绝,标准用户 -> 拒绝)上进行了测试。

static int Main(string[] args)
{
    if (!HasAdministratorPrivileges())
    {
        Console.Error.WriteLine("Access Denied. Administrator permissions are " +
            "needed to use the selected options. Use an administrator command " +
            "prompt to complete these tasks.");
        return 740; // ERROR_ELEVATION_REQUIRED
    }

    ...
    return 0;
}

private static bool HasAdministratorPrivileges()
{
    WindowsIdentity id = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(id);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

Scott's answer will do what you asked, but Microsoft recommends that console applications display an "access denied" message rather than prompt for elevation.

From http://msdn.microsoft.com/en-us/library/bb756922.aspx:

A console application presents its output on the console window and not
with a separate user interface. If an application needs a full administrator
access token to run, then that application needs to be launched from
an elevated console window.

You must do the following for console applications:

  1. Mark that your application “asInvoker”: You can do this by authoring the manifest of your application in which you set RequestedExecutionLevel == asInvoker. This setup allows callers from non-elevated contexts to create your process, which allows them to proceed to step 2.

  2. Provide an error message if application is run without a full administrator access token: If the application is launched in a non-elevated console, your application should give a brief message and exit. The recommended message is: "Access Denied. Administrator permissions are needed to use the selected options. Use an administrator command prompt to complete these tasks."

The application should also return the error code ERROR_ELEVATION_REQUIRED upon failure to launch to facilitate scripting.

My C# code for this is below. It is tested on Windows XP (administrator -> ok, standard user -> denied) and Windows Server 2008 (elevated administrator -> ok, non-elevated administrator -> denied, standard user -> denied).

static int Main(string[] args)
{
    if (!HasAdministratorPrivileges())
    {
        Console.Error.WriteLine("Access Denied. Administrator permissions are " +
            "needed to use the selected options. Use an administrator command " +
            "prompt to complete these tasks.");
        return 740; // ERROR_ELEVATION_REQUIRED
    }

    ...
    return 0;
}

private static bool HasAdministratorPrivileges()
{
    WindowsIdentity id = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(id);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
深巷少女 2024-07-15 06:29:12

您需要将 UAC 清单嵌入为嵌入式 Win32 资源。 请参阅将 UAC 清单添加到托管代码

简而言之,您使用 Windows SDK 命令行工具将其嵌入到可执行文件中。

您可以通过将以下行作为构建后任务放置在 VS 项目的属性中,将其作为构建后步骤自动执行:

mt.exe -manifest "$(ProjectDir)$(TargetName).exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1"

You need to embed the UAC manifest as an embedded Win32 resource. See Adding a UAC Manifest to Managed Code.

In short, you use a Windows SDK command line tool to embed it into your executable.

You can automate this as a post-build step by placing the following line as a post build task in your VS project's properties:

mt.exe -manifest "$(ProjectDir)$(TargetName).exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文