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);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s