.NET - 试图确定用户是否为 运行 管理员的问题
.NET - Issue trying to figure out if user is running ADMIN or not
我正在尝试弄清楚如何确定程序是否为 运行 管理员模式。我在下面展示了一些我在 .NET 中使用的编码示例:
Imports System.Security.Principal
Module Module1
Sub Main()
Dim id = WindowsIdentity.GetCurrent()
Dim pr = New WindowsPrincipal(id)
Dim isAdm As Boolean = pr.IsInRole(WindowsBuiltInRole.Administrator)
If isAdm = True Then
MessageBox.Show(Me, "Running Admin")
else
MessageBox.Show(Me, "Not Running Admin")
End If
End Sub
End Module
这在大多数情况下都很有效,但我有一个用户是 运行 Windows 7 Professional,无论他 运行 是否作为管理员。
我不知道是什么原因导致的,但是有没有办法弄清楚为什么会发生这种情况,并且可能有解决方案。要么弄清楚程序总是 return true 而不管通过编码,或者也许是解决这个问题的编码。
有什么线索吗?
我不知道 .NET 方式;但我可以给你本机代码,然后你可以 P/Invoke 调用:
/*
This function returns true if you are currently running with admin privileges.
In Vista and later, if you are non-elevated, this function will
return false (you are not running with administrative privileges).
If you *are* running elevated, then IsUserAdmin will return true,
as you are running with admin privileges.
Windows provides this similar function in Shell32.IsUserAnAdmin.
But that function is depricated.
This code is adapted from from the docs for CheckTokenMembership:
http://msdn.microsoft.com/en-us/library/aa376389.aspx
Note:
- you can be an administrator and not have admin privileges
(function returns false)
- you can have admin privileges even though you're not an administrator
(function returns true)
Any code released into the public domain. No attribution required.
*/
Boolean IsUserAdmin()
{
Boolean isAdmin;
PSID administratorsGroup = StringToSid("S-1-5-32-544"); //well-known sid
if (!CheckTokenMembership(0, administratorsGroup, out isAdmin)
isAdmin = false;
FreeSid(administratorsGroup);
return isAdmin;
}
注意:对管理员组使用 CheckTokenMembership 与其他代码非常不同。其他代码:
- 使用
OpenProcessToken
获得"me"令牌
- 使用
GetTokenInformation
和 TokenGroups
得到 TOKEN_GROUPS
,它列出了用户所属的所有组
- 迭代返回的组,使用
EqualSid
将它们与管理员 SID 进行比较
这是错误的,因为:
You can be a member of the administrators group, but not have administrator privelages!
此代码可用于了解您是否可以提升;而 IsUserAdmin 告诉您您是否 升高。
同样,您可以拥有管理员权限,但不能成为管理员组的成员。使用 IsUserAdmin()
查看您当前是否 实际上 拥有管理权限。
我正在尝试弄清楚如何确定程序是否为 运行 管理员模式。我在下面展示了一些我在 .NET 中使用的编码示例:
Imports System.Security.Principal
Module Module1
Sub Main()
Dim id = WindowsIdentity.GetCurrent()
Dim pr = New WindowsPrincipal(id)
Dim isAdm As Boolean = pr.IsInRole(WindowsBuiltInRole.Administrator)
If isAdm = True Then
MessageBox.Show(Me, "Running Admin")
else
MessageBox.Show(Me, "Not Running Admin")
End If
End Sub
End Module
这在大多数情况下都很有效,但我有一个用户是 运行 Windows 7 Professional,无论他 运行 是否作为管理员。
我不知道是什么原因导致的,但是有没有办法弄清楚为什么会发生这种情况,并且可能有解决方案。要么弄清楚程序总是 return true 而不管通过编码,或者也许是解决这个问题的编码。
有什么线索吗?
我不知道 .NET 方式;但我可以给你本机代码,然后你可以 P/Invoke 调用:
/*
This function returns true if you are currently running with admin privileges.
In Vista and later, if you are non-elevated, this function will
return false (you are not running with administrative privileges).
If you *are* running elevated, then IsUserAdmin will return true,
as you are running with admin privileges.
Windows provides this similar function in Shell32.IsUserAnAdmin.
But that function is depricated.
This code is adapted from from the docs for CheckTokenMembership:
http://msdn.microsoft.com/en-us/library/aa376389.aspx
Note:
- you can be an administrator and not have admin privileges
(function returns false)
- you can have admin privileges even though you're not an administrator
(function returns true)
Any code released into the public domain. No attribution required.
*/
Boolean IsUserAdmin()
{
Boolean isAdmin;
PSID administratorsGroup = StringToSid("S-1-5-32-544"); //well-known sid
if (!CheckTokenMembership(0, administratorsGroup, out isAdmin)
isAdmin = false;
FreeSid(administratorsGroup);
return isAdmin;
}
注意:对管理员组使用 CheckTokenMembership 与其他代码非常不同。其他代码:
- 使用
OpenProcessToken
获得"me"令牌 - 使用
GetTokenInformation
和TokenGroups
得到TOKEN_GROUPS
,它列出了用户所属的所有组 - 迭代返回的组,使用
EqualSid
将它们与管理员 SID 进行比较
这是错误的,因为:
You can be a member of the administrators group, but not have administrator privelages!
此代码可用于了解您是否可以提升;而 IsUserAdmin 告诉您您是否 升高。
同样,您可以拥有管理员权限,但不能成为管理员组的成员。使用 IsUserAdmin()
查看您当前是否 实际上 拥有管理权限。