user844541
user844541

Reputation: 2958

Cannot detect if a user is running with elevated privileges, when no UAC popup

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?

Upvotes: 6

Views: 4057

Answers (2)

user844541
user844541

Reputation: 2958

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.

Upvotes: 10

Hussein Khalil
Hussein Khalil

Reputation: 1401

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.

Upvotes: 0

Related Questions