How to find if a user is administrator or not

It’s quite common to find out whether a user or application is running Admin account or not.
well I’ve already posted a article entitled “Run Application Only in Administrator Account”. Have a look there

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

In addition we can also some other way if we are using Visual Studio 2010 by adding an application manifest file. In you’re using VS 2010, there’s an option for one in the “Add New Item” dialog (right click on the project, click “Add” then click “New Item”) and then we need to edit the generated file to uncomment the line that says:

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

The above procedure will not only forces the UAC dialog to open when you run the application, it also puts a little shield over your icon (well, in Windows 7 anyway).
You can find more details from there-MSDN[Create and Embed an Application Manifest (UAC)]

In addition I’ve posted similar tips at some different community forum. Have a look there
DotNetFunda[Run application only in Admin account]
AlternateTip-[How to find if a user is administrator or not]

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C#, Tips&Tricks and tagged , , . Bookmark the permalink.

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