简体   繁体   English

检查程序是否通过“以管理员身份运行”启动

[英]Check if a program is started via "Run as Administrator" or not

I wanted to detect if my program is started via "Run as Administrator" or not.我想检测我的程序是否通过“以管理员身份运行”启动。 On two machines it works by doing:在两台机器上,它通过以下方式工作:

new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)

Because when I run programs, by default they don't run as Administrator, so I can check it like that.因为当我运行程序时,默认情况下它们不会以管理员身份运行,所以我可以这样检查。

However on yet another machine (this one has Win7 and UAC disabled) and when I start any program there it automatically starts with admin rights, without ever having to "Right click -> Run as Administrator".但是在另一台机器上(这台机器禁用了 Win7 和 UAC),当我在那里启动任何程序时,它会自动以管理员权限启动,而无需“右键单击 -> 以管理员身份运行”。 Can I somehow filter this instance out?我可以以某种方式过滤掉这个实例吗?

Background is simply, that on the very first start it should prevent the user from starting it elevated, but if the user starts it elevated every time anyways it shouldn't care.背景很简单,在第一次启动时,它应该阻止用户启动它,但如果用户每次启动它都会提升它,它不应该关心。

Hope it makes sense, Matty希望这是有道理的,马蒂

I found out that the "EnableLUA" setting in the Registry in "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" was actually set to 0 so UAC is completely disabled and starts everything as an Admin so I will just add a check for this value to be 1 additionally:)我发现“HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System”中注册表中的“EnableLUA”设置实际上设置为 0,因此 UAC 被完全禁用并以管理员身份启动所有内容,所以我将添加一个另外检查此值是否为 1 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM