Solved

Need to know if user is administrator

Posted on 2011-03-02
3
353 Views
Last Modified: 2013-12-17
How can I tell if the user is an administrator?
0
Comment
Question by:rmmarsh
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 35018378
To detect this you can get an object of WindowsIdentity like this:

WindowsIdentity identity = WindowsIdentity.GetCurrent();

Then create an instance of WindowsPrincipan by :

WindowsPrincipal principal = new WindowsPrincipal(identity);

and finally check it by using IsInRole() method like this:

string role = "BUILTIN\\Administrators";
bool IsAdmin = principal.IsInRole(role));

then you can use the IsAdmin variable to determine whether the current user is an Admin or not.
from http://csharptuning.blogspot.com/2007/09/detecting-is-current-user-is.html
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 35018384
another one:
//namespace imports
using System.Security.Principal;

/// <summary>
/// method for checking to see if the logged in user
/// is in the Administrator's group
/// </summary>
/// <returns></returns>
public bool IsUserAdministrator()
{
    //bool value to hold our return value
    bool isAdmin;
    try
    {
        //get the currently logged in user
        WindowsIdentity user = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(user);
        isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    catch (UnauthorizedAccessException ex)
    {
        isAdmin = false;
        MessageBox.Show(ex.Message);
    }
    catch (Exception ex)
    {
        isAdmin = false;
        MessageBox.Show(ex.Message);
    }
    return isAdmin;
}

Open in new window

0
 

Author Closing Comment

by:rmmarsh
ID: 35018842
Thank you...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now