-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Background and motivation
Some code is meant to be executed as an administrator. This API makes it easy to check if the process is considered to have administrator privileges.
API Proposal
namespace System;
static class Environment
{
public static bool IsPrivilegedProcess { get; }
}Comments
-
On Unix, this would check if
geteuid() == 0.AdminHelpershas aIsProcessElevatedmethod which may have the appropriate implementation for Windows also.public static unsafe bool IsProcessElevated() -
When searching open-source code that implements this for Linux, about half of the cases implement this using
getuidinstead ofgeteuid, so they're likely getting it wrong. Having an API would make it easy to get right.
API Usage
if (Environment.IsPrivilegedProcess)
{
Console.WriteLine("Trust me, I know what I'm doing.");
}Alternative Designs
It could be on the Process class, but that class essentially represents an arbitrary process, and we do not plan to implement (nor do we see a need) to gather this info for a different process. Also, we already have Environment.Is64BitProcess
We considered various names -- the goal is to have something that doesn't appear to be specific to *nix or specific to Windows (using terms such as root, superuser, administrator, or elevated)
Risks
No response