Solved

Need to know if user is administrator

Posted on 2011-03-02
3
369 Views
Last Modified: 2013-12-17
How can I tell if the user is an administrator?
0
Comment
Question by:rmmarsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

621 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