检查当前用户是否为管理员
我的应用程序需要运行一些脚本,并且我必须确保运行它们的用户是管理员...使用 C# 执行此操作的最佳方法是什么?
My application needs to run some scripts, and I must be sure that the user running them is an administrator... What is the best way of doing this using C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您还可以调用 Windows API 来执行此操作:
它更一般地告诉您用户是否在提升的权限下运行。
You can also call into the Windows API to do this:
which more generically tells you whether user is running under elevated rights.
上面的 IsInRole 答案实际上是正确的:它确实检查当前用户是否具有管理员权限。
然而,
(来自 MSDN,例如 https:/ /msdn.microsoft.com/en-us/library/system.diagnostics.eventlogpermission(v=vs.110).aspx)
因此,IsInRole 将默认情况下考虑用户权限,因此该方法返回 false。仅当软件明确以管理员身份运行时才正确。
另一种方法检查 https://ayende.com/blog/158401/are 中的 AD -you-an-administrator 将检查用户名是否在管理员组中。
我将两者结合起来的完整方法是这样的:
对于没有提升权限的管理组中的用户(启用UAC),此方法 IsCurrentUserAdmin() return !checkCurrentRole: true if checkCurrentRole==false, but false if checkCurrentRole==true
如果运行代码需要管理员权限,请考虑 checkCurrentRole==true。否则,届时您将收到安全异常。
因此,正确的 IsInRole 逻辑。
The above answers with IsInRole are actually correct: it does check if the current user has admin privilege.
However,
(from MSDN, e.g. https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlogpermission(v=vs.110).aspx)
Thus, IsInRole will per default consider the user privilege, and thus the method return false. Only true when the software is explicitly run as administrator.
The other method checking AD in https://ayende.com/blog/158401/are-you-an-administrator will check if the user name is in an admin group.
My complete method combining both is thus:
For a user in an admin group without elevated privilege (UAC enabled), this method IsCurrentUserAdmin() return !checkCurrentRole: true if checkCurrentRole==false, but false if checkCurrentRole==true
If you run code that REQUIRES admin privilege, consider the checkCurrentRole==true. Otherwise, you'll get a security exception by then.
Therefore the correct IsInRole logic.
与这里的其他程序一样,我的程序没有以提升的方式运行,因此如果启用了 UAC,此代码将返回
false
:@EricBDev 的
IsAdministratorNoCache
答案确实返回true
如果我的程序没有运行提升,并且用户是管理员。然而,正如博客作者所说,它非常慢。这是我的解决方案;它模拟
IsAdministratorNoCache
但速度很快:Like others here, my program is not running elevated, so this code returns
false
if UAC is enabled:@EricBDev's answer with
IsAdministratorNoCache
does returntrue
if my program is not running elevated, and the user is an Admin. However, like the blog author says, it is very slow.Here is my solution; it emulates
IsAdministratorNoCache
but is fast:David Ching 的答案很好 - 但给您留下了管理员组的硬编码 ID。这有点清楚了:
The answer by David Ching is good - but leaves you with a hardcoded ID for the Admin group. This is a bit clearer:
只是想我会添加另一个解决方案;因为
IsInRole
并不总是有效。根据您的需要是否需要支持旧系统;或者不确定您的客户如何物理管理您的系统。这是我实施的解决方案;以实现灵活性和变更。
所以上面的代码有一些结构;它实际上会测试用户是否使用 Vista 或更高版本。这样,如果客户使用的是多年前没有框架或测试版框架的 XP,它将允许您更改您想要执行的操作。
然后它将进行物理测试以避免帐户出现空值。
最后,它将提供检查以验证用户确实处于正确的角色。
我知道问题已经得到解答;但我认为我的解决方案对于其他正在搜索 Stack 的人来说将是一个很好的补充。我对受保护构造函数的推理将允许您使用此类作为派生类,您可以控制该类实例化时的状态。
Just thought I'd add another solution; as the
IsInRole
doesn't always work.Depending on your needs if you need to support older systems; or are unsure of how your client is physically managing your system. This is a solution I implemented; for flexibility and alterations.
So the above code has a few constructs; it will actually test to see if the User is on Vista or higher. That way if a customer is on XP without a framework or beta framework from years ago it will allow you to alter what you'd like to do.
Then it will physically test to avoid a null value for the account.
Then last of all it will provide the check to verify that the user is indeed in the proper role.
I know the question has been answered; but I thought my solution would be a great addition to the page for anyone else whom is searching Stack. My reasoning behind the Protected Constructor would allow you to use this class as a Derived Class that you could control the state of when the class is instantiated.
如果您的应用程序必须以管理员权限运行,则更新其清单是正确的。
将
requestedExecutionlevel
设置为requireAdminstrator
。If your application must be run with admin rights, it would be right to update its manifest.
Set
requestedExecutionlevel
torequireAdminstrator
.这就是我的结局...
我强制我的应用程序以管理员模式运行。
为此,请执行以下操作:
1- 将
app.manifest
添加到您的csproj
文件中。MyProject.csproj
2- 将以下
app.manifest
文件添加到您的项目中。应用程序清单
This is how I end up...
I'm forcing my app to run as administrator mode.
To do this
1- Add
<ApplicationManifest>app.manifest</ApplicationManifest>
to yourcsproj
file.MyProject.csproj
2- Add the below
app.manifest
file to your project.app.manifest