Is user Administrator in c#

Y

Yoavo

Hi,
I need to know if the current user is administrator.
The trivial methods does not work on Vista.

What is the correct way to get it ?

Yoav.
 
P

Patrice

Please be specific. Is WindowsPrincipal.IsInRole the trivial method you are
talking about ? Also define "does not work" ? What is your app behavior ?

I'm not that familiar with Vista and I don't have permanent access to Vista
but IMO it could be an UAC issue. That is even if the user is in the admin
group it doesn't mean that *at this time* it runs under the privilege you
need. You perhaps needs to add a manifest to your application to trigger the
UAC dialog...
 
Y

Yoavo

This is the code I am using:

static bool IsAdmin()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(
PrincipalPolicy.WindowsPrincipal);

// read current identity for this thread
WindowsPrincipal principal =
(WindowsPrincipal)Thread.CurrentPrincipal;
WindowsIdentity identity = (WindowsIdentity)principal.Identity;
// can the Administrator role be found?
bool isAdmin =
principal.IsInRole(WindowsBuiltInRole.Administrator);
return isAdmin;
}

It return false for a user that is an administrator.
I cant add manifest to my application because the program should run also
for users which are not administrators.
 
W

Wilson, Phil

A manifest doesn't mandate requiresAdministrator. It's possible that
level=highestavailable would work for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top