Check for administrator privileges in C#
Asked Answered
S

3

55

I want to know if a program is running as administrator.

The user doesn't have to be administrator. I only want to know if my application has rights to edit some secured files that are editable when running as Administrator.

Society answered 10/5, 2011 at 16:27 Comment(2)
possible duplicate of How can I tell if my process is running As Administrator?Wittol
I just Googled that question and it looks like I asked it 3 years ago. Good to know. Thanks Google.Society
S
123

This will return a bool valid

using System.Security.Principal;

bool isElevated;
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
Scintillant answered 10/5, 2011 at 16:32 Comment(2)
+1 because that even works on Linux (Mono). Gives you true when ran as root/via sudo. Gives false as a default user.Bang
If you build for .NET 5.0, you get: error CA1416: This call site is reachable on all platforms. 'WindowsBuiltInRole.Administrator' is only supported on: 'windows'.Underpinning
N
12

Here's @atrljoe's answer turned into a one liner using the latest C#:

using System.Security.Principal;

static bool IsElevated => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
Nanna answered 12/11, 2015 at 23:51 Comment(8)
This is ugly. I'm sorry. But really, you should shorten everythiing. It is bette to leave it as 4 lines and make function out of it. How to even debug your one liner?Society
I believe that terseness is a vitue, as long as it's clear.Nanna
Interesting... I find this easier to read than the expanded version. This has a clear call hierarchy, whereas to understand code exploded into different variables, I need to mentally execute it.Disharmonious
Another downside of this form is that you never call Dispose as you are required by contract to doBedpan
My downvote is because it isn't disposed of, as Ian said.Bombe
Not only is it ugly, it's not clear what it does. I'm in the "clear and concise even if it takes more lines of code" boat.Bouquet
This exact code would be ten times more readable if it were just made multi-line. Comments won't allow formatting, but if you insert a carriage return directly after the => operator, and after the Windows Principle constructor call, readability would be vastly better. Irrelevant, though, because you really do have to dispose that WindowsIdentity.Underpinning
You're not disposing it...Filigree
P
2

To avoid SecurityException, and leaks, like not closing WindowsIdentity.GetCurrent(), since WindowsPrincipal does not explicitly close, but only creates a new connection and only then closes it.

private static bool IsAdministrationRules() {
   try {
       using (WindowsIdentity identity = WindowsIdentity.GetCurrent()) {
          return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);
       }
   } catch {
       return false;
   }
}
Pessa answered 28/10, 2022 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.