Skip to content

[API Proposal]: Add API to check if process had administrator privileges #68770

@tmds

Description

@tmds

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. AdminHelpers has a IsProcessElevated method 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 getuid instead of geteuid, 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions