C#: Test For Elevated Privileges

So you want to know if your current code is has been “run as administrator”? You’ll need to check if the current Windows User has the built-in role “Administrator”. To make things short, here you go:

using System.Security.Principal;

// ...

var windowsIdentity = WindowsIdentity.GetCurrent();
var windowsPrincipal = new WindowsPrincipal(windowsIdentity );
var hasElevatedPrivileges = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);

Freelance full-stack .NET and JS developer and architect. Located near Cologne, Germany.

Leave a Reply

Your email address will not be published. Required fields are marked *