Skip to content

Instantly share code, notes, and snippets.

@jdmallen
Created May 15, 2021 18:03
Show Gist options
  • Save jdmallen/4c734c5e914836f5af408890cbf7746b to your computer and use it in GitHub Desktop.
Save jdmallen/4c734c5e914836f5af408890cbf7746b to your computer and use it in GitHub Desktop.
Check if .NET Core/5+ app is running elevated
// 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