Get the App
SLTechnology News&Howtos  ›  Development  › 

How to tell if the .NET Core application is running as an administrator

Shulou Source: shulou.com Published: 2022-06-01 10:36:17 10月15日 Update

This article mainly introduces the relevant knowledge of how to judge that the .NET Core application is running as an administrator. The content is detailed and easy to understand, easy to operate, and has certain reference value. I believe you will gain something after reading this article on how to judge that the .NET Core application is running as an administrator. Let's take a look.

Sometimes we need to know whether the current program is running as an administrator in order to perform operations that require special permissions.

A solution has been found on github:

/ / need to reference nuget package Mono.Posix.NETStandardpublic static bool IsAdministrator = > RuntimeInformation.IsOSPlatform (OSPlatform.Windows)? New WindowsPrincipal (WindowsIdentity.GetCurrent ()) .IsInRole (WindowsBuiltInRole.Administrator): Mono.Unix.Native.Syscall.geteuid () = 0

The judgment under Windows is easy to understand that the user belongs to the Administrator role. But what does geteuid () = = 0 under linux mean?

1.euid

Each process in the linux system has two ID, which are the user ID (uid) and the valid user ID (euid).

The user ID of root is indeed 0.

Console.WriteLine ($"UID: {Mono.Unix.Native.Syscall.getuid ()}"); Console.WriteLine ($"EUID: {Mono.Unix.Native.Syscall.geteuid ()}")

Then why do you need two Id?

2.setuid

Because the linux system has a function: if a program is set to setuid bit, no matter which user it is executed, it will have the permissions of the corresponding user of setuid, and the EUID of the process will become the UID of the corresponding user.

3.Demo

Let's try it. By default, demo1 users cannot operate 1.txt because the files are created by root users, as shown below:

At this point, we run the program again and find that the 1.txt operation is successful and the euid becomes 0 (root user).

This is the end of the article on "how to determine that a .NET Core application is running as an administrator. Thank you for reading!" I believe that everyone has a certain understanding of "how to judge that .NET Core applications run as administrators". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Users programs runners administrators identities management applications applications knowledge content permissions articles systems processes valid special successful value again function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Shulou Information Huawei OPPO Reno