Created
May 15, 2021 18:03
-
-
Save jdmallen/4c734c5e914836f5af408890cbf7746b to your computer and use it in GitHub Desktop.
Check if .NET Core/5+ app is running elevated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To check if you are running as root on Unix: | |
// 1. Add a PackageReference to Mono.Posix.NETStandard | |
// 2. Change IsAdminstrator to the following: | |
public static bool IsAdministrator => | |
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | |
? new WindowsPrincipal(WindowsIdentity.GetCurrent()) | |
.IsInRole(WindowsBuiltInRole.Administrator) | |
: Mono.Unix.Native.Syscall.geteuid() == 0; | |
// Source: https://github.com/dotnet/runtime/issues/25118#issuecomment-367407469 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment