Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

uac

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?

like image 484
user844541 Avatar asked Aug 14 '12 15:08

user844541


1 Answers

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.

like image 94
user844541 Avatar answered Sep 19 '22 03:09

user844541