6

I am developing a C# application that needs to detect whether the user is running as elevated administrator. I tried the solution suggested by Steven.

I checked the solution on 5 machines and it works fine on 4 of them.

There is one machine that never show the UAC notification message, even though I set the User Account Control to Always notify me.

When checking the code on that machine, even if I choose run as administrator, it doesn't work and the method IsProcessElevated returns false.

Is something wrong with that specific machine?

Is there a way to determine whether a user is elevated or not, on that kind of platform?

Community
  • 1
  • 1
user844541
  • 2,868
  • 5
  • 32
  • 60
  • I would check that the machines has all the latest service packs etc and then try again. – Gaz Winter Aug 14 '12 at 15:10
  • there are some alternative solutions to check UAC [here](http://stackoverflow.com/questions/95912/how-can-i-detect-if-my-process-is-running-uac-elevated-or-not). Is it possible to try one of those and see if that works? – default Aug 14 '12 at 15:14
  • 1
    It sounds to me like there's something wrong with that computer, not the code. You should be getting UAC prompts if you explicitly configure it to prompt. You're not. That points to a problem with the system itself. – Cody Gray - on strike Aug 14 '12 at 19:40
  • Did you try running other programs that require to be elevated on that machine? Do you get an UAC prompt for them? Do they work as expected? – dureuill Aug 30 '12 at 08:37

2 Answers2

10

After a research I found out that from Windows 7 and on in order to determine whether a user is an elevated admin or not you just need to check the following:

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole (WindowsBuiltInRole.Administrator);

(The rest of the code that checks the Token can be used in case this option fails) I tested the code on some of the machines and it works ok.

my application runs only on win7 and above so this solution is good enough for me.

user844541
  • 2,868
  • 5
  • 32
  • 60
0

It might be silly to ask, but after you changed the UAC to always notify me, have you restarted the machine?

I had the same issue and the problem was in Windows itself as the UAC status is not changed unless you restart the machine.

Give it a try, and also check this link and this.

Community
  • 1
  • 1
Hussein Khalil
  • 1,395
  • 11
  • 29