Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if my process is running as Administrator?

I would like to display some extra UI elements when the process is being run as Administrator as opposed to when it isn't, similar to how Visual Studio 2008 displays 'Administrator' in its title bar when running as admin. How can I tell?

like image 278
Erik Forbes Avatar asked Feb 03 '09 22:02

Erik Forbes


People also ask

How do I know if I am running as administrator Windows 10?

Right-click the name (or icon, depending on the version Windows 10) of the current account, located at the top left part of the Start Menu, then click on Change account settings. The Settings window will pop up and under the name of the account if you see the word "Administrator" then it is an Administrator account.

How do I know if I am running as administrator in CMD?

You should use "net session" command and look for an error return code of "0" to verify administrator rights.

How do I check if Windows is administrator?

Select Start, and select Control Panel. In the Control Panel window, select User Accounts and Family Safety > User Accounts > Manage User Accounts. In the User Accounts window, select Properties and the Group Membership tab. Make sure Administrator is selected.


2 Answers

Technically, if you want to see if the member is the local administrator account, then you can get the security identifier (SID) of the current user through the User property on the WindowsIdentity class, like so (the static GetCurrent method gets the current Windows user):

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();  string sid = windowsIdentity.User.ToString(); 

The User property returns the SID of the user which has a number of predefined values for various groups and users.

Then you would check to see if the SID has the following pattern, indicating it is the local administrator account (which is a well-known SID):

S-1-5-{other SID parts}-500

Or, if you don't want to parse strings, you can use the SecurityIdentifier class:

// Get the built-in administrator account. var sid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid,      null);  // Compare to the current user. bool isBuiltInAdmin = (windowsIdentity.User == sid); 

However, I suspect that what you really want to know is if the current user is a member of the administrators group for the local machine. You can get this SID using the WellKnownSidType of BuiltinAdministratorsSid:

// Get the SID of the admin group on the local machine. var localAdminGroupSid = new SecurityIdentifier(     WellKnownSidType.BuiltinAdministratorsSid, null); 

Then you can check the Groups property on the WindowsIdentity of the user to see if that user is a member of the local admin group, like so:

bool isLocalAdmin = windowsIdentity.Groups.     Select(g => (SecurityIdentifier) g.Translate(typeof(SecurityIdentifier))).     Any(s => s == localAdminGroupSid); 
like image 186
casperOne Avatar answered Oct 09 '22 03:10

casperOne


I think this is a good simple mechanism.

using System.Security.Principal;  WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); 
like image 34
Kenn Avatar answered Oct 09 '22 03:10

Kenn