65.9K
CodeProject is changing. Read more.
Home

How to find if a user is administrator or not

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Dec 6, 2011

CPOL
viewsIcon

6378

It's quite common to find out whether a user or application is running Admin account or not.CodeProject already has excellent tips focusing a nice way to accomplish the same task.Have a look at the CodeProject links.Tips posted by John Is Your App Running As Administrator?[^] and Sam...

It's quite common to find out whether a user or application is running Admin account or not. CodeProject already has excellent tips focusing a nice way to accomplish the same task. Have a look at the CodeProject links. Tips posted by John Is Your App Running As Administrator?[^] and Sam Cragg gives an excellent alternate tip to John's tip. Sam tries to focus on how to use the application manifest file (It works only in VS2010).
<requestedexecutionlevel level="requireAdministrator" uiaccess="false" />
Alternate Tip Link: Is Your App Running As Administrator?(AlternateTip By Cragg)[^] Reference Link: http://msdn.microsoft.com/en-us/library/bb756929.aspx[^] In addition, I've also posted my code in another forum which focuses on the same concept. Admin Check[^] In addition to the above reference, you can use the given code also to find out whether the user is Admin or not. Just use System.DirectoryServices.AccountManagement:
  public static bool IsAdmin()
        {
            bool isAllowed = false;
            using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
            {
                UserPrincipal up = UserPrincipal.Current;
                GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators");
                if (up.IsMemberOf(gp))
                    isAllowed = true;
            }
            return isAllowed;
        }
Reference Link: MSDN-[System.DirectoryServices.AccountManagement][^]