简体   繁体   English

以管理员身份运行的 WinUI 应用程序?

[英]WinUI application running as Administrator?

I'm trying to write a WinUI 3 desktop app the requires the Administrator role.我正在尝试编写一个需要管理员角色的 WinUI 3 桌面应用程序。 It does some msiexec work on behalf of the user, and this pretty much demands being admin as far as I can tell.它代表用户执行一些 msiexec 工作,据我所知,这非常需要管理员身份。

The app itself will be an unpackaged ( <WindowsPackageType>None</WindowsPackageType> ) and self-contained ( <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained> ).应用程序本身将是未打包的 ( <WindowsPackageType>None</WindowsPackageType> ) 和独立的 ( <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained> )。 Both of these settings are in the.csproj file.这两个设置都在 .csproj 文件中。

To try and get Administrator role, I've added this to the app.manifest file:为了尝试获得管理员角色,我将其添加到app.manifest文件中:

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  <security>
    <requestedPrivileges>
      <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
    </requestedPrivileges>
  </security>
</trustInfo>

as part of the application element.作为应用程序元素的一部分。 I understand that this should be sufficient to ensure that either the application is being run "as Administrator", or pops up the UAC dialog to request such permissions.我知道这应该足以确保应用程序正在“以管理员身份”运行,或者弹出 UAC 对话框以请求此类权限。

To check whether we are indeed running as Administrator, I've added this function which is called to check on admin status:为了检查我们是否确实以管理员身份运行,我添加了这个 function 来检查管理员状态:

    public static bool IsAdmin()
    {
        var identity = WindowsIdentity.GetCurrent();
        var principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
  • If I build my app and run it outside of Visual Studio, it reports that it is not running as Administator.如果我构建我的应用程序并在 Visual Studio 之外运行它,它会报告它没有以管理员身份运行。 No UAC, just runs the app as a standard user (if that's the correct terminology).没有 UAC,只是以标准用户身份运行应用程序(如果这是正确的术语)。

  • If I right-click and select the Run as administrator option, then the app runs and reports that it is running as Administrator.如果我右键单击 select以管理员身份运行选项,则该应用程序运行并报告它正在以管理员身份运行。

  • If I run it in the Visual Studio debugger, it's always running as Administrator.如果我在 Visual Studio 调试器中运行它,它总是以管理员身份运行。 But I do run Visual Studio as admin, so maybe it's just picking that up from the parent process.但我确实以管理员身份运行 Visual Studio,所以它可能只是从父进程中获取它。 Not sure.不确定。

So the question is why does just running the app normally neither run as Administrator, nor pop up the UAC dialog to elevate itself to that state?所以问题是为什么只是运行应用程序通常既不会以管理员身份运行,也不会弹出 UAC 对话框将自己提升到 state? Am I doing something fundamentally wrong in the app.manifest file?我在 app.manifest 文件中做错了什么吗? Should it work?它应该工作吗?

Very much a novice when it comes to manifests, C#, WinUI etc, so please be patient if I'm making invalid assumptions or I don't quite understand your answer.在清单、C#、WinUI 等方面,我是一个新手,所以如果我做出的假设无效或者我不太理解您的回答,请耐心等待。 Happy to fill in any blanks that I've missed.很高兴填补我遗漏的任何空白。

I'm not sure if elevated unpackaged apps are supported at the latest WinAppSDK v1.2.我不确定最新的 WinAppSDK v1.2 是否支持提升的未打包应用程序。 But dropping WindowsApSDKSelfContained seems to work.但是删除WindowsApSDKSelfContained似乎可行。

This is what I tried:这是我试过的:

  1. Create a plain WinUI 3 app and make it unpackaged.创建一个普通的 WinUI 3 应用程序并将其解包。 (No self-contained) (不自成体系)
<WindowsPackagedType>None</WindowsPackagedType>
<!--<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>-->
  1. Edit Package.appxmanifest.编辑 Package.appxmanifest。
<Capabilities>
  <rescap:Capability Name="runFullTrust" />
  <rescap:Capability Name="allowElevation" />
</Capabilities>
  1. Edit app.manifest.编辑 app.manifest。
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  <security>
    <requestedPrivileges>
      <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    </requestedPrivileges>
  </security>
</trustInfo>
  1. Add your code to check if it's running as administrator.添加您的代码以检查它是否以管理员身份运行。 MainWindow.xaml主窗口.xaml
<Window
    x:Class="ElevationTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid>
        <TextBlock x:Name="AdministratorStatusTextBlock"/>
    </Grid>
</Window>

MainWindow.xaml.cs MainWindow.xaml.cs

using Microsoft.UI.Xaml;
using System.Security.Principal;

namespace ElevationTest;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        AdministratorStatusTextBlock.Text = IsAdmin() is true
            ? "Running as admin."
            : "NOT running as admin.";
    }

    public static bool IsAdmin()
    {
        var identity = WindowsIdentity.GetCurrent();
        var principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
}
  1. Build in Release mode and run the created *.exe file.在 Release 模式下构建并运行创建的 *.exe 文件。

The app should ask for permission and the text should show "Running as admin.".该应用程序应请求许可,文本应显示“以管理员身份运行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以管理员身份运行 DLL - Running a DLL as administrator Delphi 7:使用管理员权限执行应用程序 - Delphi 7: Execute application with administrator privileges 如何使我的应用程序自动以管理员权限运行? - How do I get my application to run with administrator rights automatically? JetBrains Rider C#应用程序需要管理员权限 - JetBrains Rider C# Application Requiring Administrator Privileges 通过mage.exe将应用程序清单文件设置为管理员特权 - application manifest file through mage.exe set to administrator privilege 为Windows 7 / Vista应用程序(VS2012 / InstallShield2013)启用“以管理员身份运行” - Enable 'Run as an Administrator' for Windows 7/Vista application (VS2012/InstallShield2013) 防止应用程序在Android平板电脑上运行 - Preventing application from running on android tablets Winform 应用程序未在 64 位服务器上运行 - Winform application not running on 64bit server 从启动器运行应用程序A时,有时也会运行应用程序B - When running application A from the launcher it sometimes also runs application B 如何访问正在运行的应用程序的应用程序清单? - How can I access the application manifest of the running application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM