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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With