Solved

Need to know if user is administrator

Posted on 2011-03-02
3
366 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Gridview 1 48
SSIS Feed MS SQL Server Stored procedure with loop 4 37
SSIS Start 2 21
PowerShell:  Column widths won't expand 3 19
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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