Search code examples
.netmanifest.net-6.0maui

.Net MAUI - Admin Privileges on Windows


Using .Net MAUI in Visual studio preview i am unable to run things that require administrator privileges for a Windows application.

I have tried the following:

  • Open Visual studio preview as an administrator
  • Setup app.manifest and .csproj with the following:

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>

.csproj

<ApplicationManifest>app.manifest</ApplicationManifest>

I have a very similar WPF application, that also runs in .net 6 where admin privileges works if you run Visual studio as an administrator.


Solution

  • You're on the right track but you need another ingredient. Open the package.appxmanifest file with a text editor and make sure that the Capabilities node looks like this

    <Capabilities>
        <rescap:Capability Name="runFullTrust" />
        <rescap:Capability Name="allowElevation" />
      </Capabilities>
    

    I've added the allowElevation one.

    Then in your app.manifest add this part right under the assembly node

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

    You can tweak the level as needed. It seems that if you set the uiAccess to true that is not supported. Also, elevation is only available from WinAppSdk 1.1+ and on:

    • Windows 11 - May 10, 2022—KB5013943 (OS Build 22000.675)
    • Windows 10 - May 10, 2022—KB5013942 (OS Builds 19042.1706, 19043.1706, and 19044.1706)

    See: https://learn.microsoft.com/windows/apps/windows-app-sdk/stable-channel#elevation