Check User Is Administrator or Not?

Description :

This code shows how we can check whether the current user is an Administrator or not.

Following is the namespace of used

using System.Security.Principal;

Following is the code to find whether the user is Administrator

bool IsAnAdministrator ()
WindowsIdentity MyIdentity =
WindowsPrincipal MyPrincipal =
new WindowsPrincipal (MyIdentity);
return MyPrincipal.IsInRole (WindowsBuiltInRole.Administrator);

Code Explanation

1. Create the instance of Windows identity.
2. Create the instance of Windows Principal.
3. Get the role



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: