我正在开发,需要检测用户是否正在作为提升的管理员C#应用程序。我试过solution由史蒂芬建议。
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.
我查了5台机器的解决方案,并能正常工作的4人。
I checked the solution on 5 machines and it works fine on 4 of them.
有一个机器,从来没有显示UAC通知消息,即使我设置用户帐户控制,以始终通知我
。
There is one machine that never show the UAC notification message, even though I set the User Account Control to Always notify me
.
在检查机器上的code,即使我选择以管理员身份运行
,这是行不通的,该方法 IsProcessElevated
返回false。
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?
研究我发现,从Windows 7和以确定用户是否升高管理员或没有你只需要检查以下后:
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);
(的code,检查令牌其余的情况下能够使用此选项失败) 我测试了code上的一些机器,它工作正常。
(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.
我的应用程序只能在WIN7及以上所以这个解决方案是对我来说已经足够了。运行
my application runs only on win7 and above so this solution is good enough for me.