dotnet核心应用以管理员身份运行

时间:2017-02-10 12:10:18

标签: c# .net-core uac

我有一个需要管理员权限才能运行的dotnet控制台应用程序。我找不到怎么做。在常规项目中,我将添加app.manifest并设置

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

但我无法想象如何在构建中嵌入它。

我该怎么做?

6 个答案:

答案 0 :(得分:8)

<ApplicationManifest>app.manifest</ApplicationManifest>添加到您的csproj文件中。

MyProject.csproj

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <ApplicationManifest>app.manifest</ApplicationManifest>
  </PropertyGroup>    
</Project>

将以下app.manifest文件添加到您的项目中。

app.manifest

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

答案 1 :(得分:6)

我发现最简单的解决方法是添加 app.manifest 文件,其设置类似于网络框架应用程序中的设置

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

然后在您的网络核心项目文件(C#项目中为.csproj)上添加以下内容:

<PropertyGroup>
  <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

*在控制台和WPF netcore 3.0中工作

答案 2 :(得分:3)

.NET Core不支持此功能。

.NET Core实现了.NET Framework功能的(扩展)子集,这些功能已知是跨平台的。

许多非Windows平台不支持请求和接收提升权限的应用程序。因此,.NET Core不支持它。

(另一方面,删除权限可能会受到更多平台的支持)

答案 3 :(得分:2)

在.NET核心中,当前app.manifest似乎已被忽略。但是,您可以检测您是否正在以管理员身份运行,并向用户提供错误消息。

在Main方法中首先调用MainClass.RequireAdministrator()作为第一件事。如果该进程不是以管理员/超级用户身份启动的,这将在Windows和Linux上给出错误消息。您可能需要添加Windows兼容性NuGet软件包才能在Windows上运行。

这不会强制抬高,但至少用户会得到一个有用的错误,告诉他们如何解决问题。

using System.Runtime.InteropServices;
using System.Security.Principal;

namespace MyNamespace
{
    public static class MainClass
    {
        public static void Main(string[] args)
        {
            RequireAdministrator();
        }

        [DllImport("libc")]
        public static extern uint getuid();

        /// <summary>
        /// Asks for administrator privileges upgrade if the platform supports it, otherwise does nothing
        /// </summary>
        public static void RequireAdministrator()
        {
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
                    {
                        WindowsPrincipal principal = new WindowsPrincipal(identity);
                        if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
                        {
                            throw new InvalidOperationException("Application must be run as administrator");
                        }
                    }
                }
                else if (getuid() != 0)
                {
                    throw new InvalidOperationException("Application must be run as root");
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to determine administrator or root status", ex);
            }
        }
    }
}

答案 4 :(得分:0)

正如奥马吉德在评论中已经指出的那样,目前无法强制提升。 但是,您仍然可以使用管理员权限运行终端(Powershell,CMD等)。这也将使用相同的权限运行您的应用程序 - 也就是 - 管理员权限。我需要为HttpListener添加前缀。

答案 5 :(得分:0)

要扩展jjxtra的答案,如果您正在运行跨平台,则显然他的答案在非Windows实例中将不起作用。我知道...“ pinvoke baaaaad”,但在这种情况下,我认为还可以,因为我没有其他选择。

因此,对于linux / mac,您可以在以下位置添加以下代码:

private static class LinuxNative
{
    [DllImport("libc")]
    public static extern uint getuid();
}

var isRoot = LinuxNative.getuid() == 0;
相关问题