The above answers with IsInRole are really correct: it checks to see if the current user has administrator rights. However,
Starting with Windows Vista, User Account Control (UAC) defines user privileges. If you are a member of the Embedded Admins group, you are assigned two access tokens at run time: the standard user access token and the administrator access token. By default, you are the standard user.
(from MSDN, for example, https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlogpermission(v=vs.110).aspx )
Thus, IsInRole defaults to user privileges, and thus the method returns false. True, only if the program is explicitly run as administrator.
Another method for checking AD at https://ayende.com/blog/158401/are-you-an-administrator will check if the username is in the administrators group.
My complete method combining both:
public static bool IsCurrentUserAdmin(bool checkCurrentRole = true) { bool isElevated = false; using (WindowsIdentity identity = WindowsIdentity.GetCurrent()) { if (checkCurrentRole) { // Even if the user is defined in the Admin group, UAC defines 2 roles: one user and one admin. // IsInRole consider the current default role as user, thus will return false! // Will consider the admin role only if the app is explicitly run as admin! WindowsPrincipal principal = new WindowsPrincipal(identity); isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator); } else { // read all roles for the current identity name, asking ActiveDirectory isElevated = IsAdministratorNoCache(identity.Name); } } return isElevated; } /// <summary> /// Determines whether the specified user is an administrator. /// </summary> /// <param name="username">The user name.</param> /// <returns> /// <c>true</c> if the specified user is an administrator; otherwise, <c>false</c>. /// </returns> /// <seealso href="https://ayende.com/blog/158401/are-you-an-administrator"/> private static bool IsAdministratorNoCache(string username) { PrincipalContext ctx; try { Domain.GetComputerDomain(); try { ctx = new PrincipalContext(ContextType.Domain); } catch (PrincipalServerDownException) { // can't access domain, check local machine instead ctx = new PrincipalContext(ContextType.Machine); } } catch (ActiveDirectoryObjectNotFoundException) { // not in a domain ctx = new PrincipalContext(ContextType.Machine); } var up = UserPrincipal.FindByIdentity(ctx, username); if (up != null) { PrincipalSearchResult<Principal> authGroups = up.GetAuthorizationGroups(); return authGroups.Any(principal => principal.Sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) || principal.Sid.IsWellKnown(WellKnownSidType.AccountDomainAdminsSid) || principal.Sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) || principal.Sid.IsWellKnown(WellKnownSidType.AccountEnterpriseAdminsSid)); } return false; }
For a user in the administrator group without elevated privileges (UAC is enabled), this IsCurrentUserAdmin () method returns! CheckCurrentRole: true if checkCurrentRole == false, but false if checkCurrentRole == true.
If you run code that REQUIRES administrator privileges, consider checkCurrentRole == true. Otherwise, you will receive a security exception by then. Therefore, the correct logic is IsRole .
EricBDev Jun 15 '17 at 10:47 on 2017-06-15 10:47
source share