C# UAC 需要控制台应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/227187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UAC need for console application
提问by Patrick Desjardins
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 :
我有一个控制台应用程序需要使用一些需要管理员级别的代码。我读过我需要添加一个清单文件 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>
But it still doesn't raise the UAC (in the console or in debugging in VS). How can I solve this issue?
但它仍然没有提高 UAC(在控制台中或在 VS 中调试)。我该如何解决这个问题?
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?
如果我在 Administrator 中运行解决方案或在 Administrator 中运行 /bin/*.exe,我就可以使其工作。我仍然想知道是否有可能在应用程序启动时弹出一些东西,而不是明确地右键单击>以管理员身份运行?
采纳答案by Judah Gabriel Himango
You need to embed the UAC manifest as an embedded Win32 resource. See Adding a UAC Manifest to Managed Code.
您需要将 UAC 清单作为嵌入式 Win32 资源嵌入。请参阅向托管代码添加 UAC 清单。
In short, you use a Windows SDK command line tool to embed it into your executable.
简而言之,您可以使用 Windows SDK 命令行工具将其嵌入到您的可执行文件中。
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:
您可以通过将以下行作为构建后任务放置在 VS 项目的属性中,将其作为构建后步骤自动执行:
mt.exe -manifest "$(ProjectDir)$(TargetName).exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1"
回答by scobi
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.
对于任何使用 Visual Studio 的人来说,这都非常简单。我正要设置 Windows SDK 并执行 mt.exe 构建后步骤以及所有这些,然后才意识到它已内置到 VS 中。我想我会为后代记录下来。
- Project | Add New Item -> Visual C# Items -> Application Manifest File
- Open app.manifest, change requestedExecutionLevel.@level to "requireAdministrator"
- Build
- 项目 | 添加新项目 -> Visual C# 项目 -> 应用程序清单文件
- 打开 app.manifest,将 requestsExecutionLevel.@level 更改为“requireAdministrator”
- 建造
Ta-da
田田
回答by Joe Daley
Scott's answer will do what you asked, but Microsoft recommends that console applications display an "access denied" message rather than prompt for elevation.
Scott 的回答将按照您的要求执行,但 Microsoft 建议控制台应用程序显示“访问被拒绝”消息,而不是提示提升权限。
From http://msdn.microsoft.com/en-us/library/bb756922.aspx:
从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:
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.
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.
控制台应用程序在控制台窗口上显示其输出,而不是使用单独的用户界面。如果应用程序需要完整的管理员访问令牌才能运行,则需要从提升的控制台窗口启动该应用程序。
您必须对控制台应用程序执行以下操作:
将您的应用程序标记为“asInvoker”:您可以通过创作您的应用程序清单来实现这一点,在该清单中您设置了 RequestedExecutionLevel == asInvoker。此设置允许来自非提升上下文的调用者创建您的流程,从而允许他们继续执行第 2 步。
如果应用程序在没有完整管理员访问令牌的情况下运行,则提供错误消息:如果应用程序在非提升的控制台中启动,您的应用程序应该给出一条简短的消息并退出。推荐的消息是:“拒绝访问。需要管理员权限才能使用所选选项。使用管理员命令提示符完成这些任务。”
应用程序还应在启动失败时返回错误代码 ERROR_ELEVATION_REQUIRED 以方便脚本编写。
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).
我的 C# 代码如下。它在 Windows XP(管理员 -> ok,标准用户 -> 拒绝)和 Windows Server 2008(高级管理员 -> ok,非高级管理员 -> 拒绝,标准用户 -> 拒绝)上进行了测试。
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);
}