如何判断用户是管理员,即使不升高 [英] How to determine if user is an Administrator, even if non-elevated
问题描述
在我的C#应用程序,我需要检查,如果当前用户是管理员组的成员。它需要与Windows XP和Windows 7兼容
In my C# application, I need to check if the current user is a member of the Administrators group. It needs to be compatible with both Windows XP and Windows 7.
目前,我使用下面的code:
Currently, I am using the following code:
bool IsAdministrator
{
get
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
问题是,如果应用程序运行在Windows 7的UAC开启作为非提升管理员此方法返回false。如何确定用户是否即使在应用程序运行作为一个非管理员提升管理员?
The problem is that this method returns false if the application is run on Windows 7 with UAC turned on as a non-elevated Administrator. How can I determine if the user is an Administrator even if the application is run as a non-elevated Administrator?
推荐答案
有一个Win32 API GetTokenInformation
,可以用来检查当前的令牌。如果返回的令牌是一个分裂的道理,它可能是运行我非提升模式的管理员用户。
There is a Win32 API GetTokenInformation
that can be used to check the current token. If the returned token is a split token, it probably is an administrator user that is running i non elevated mode.
GetTokenInformation
有一个输出参数 tokenInformation
这需要三个值中的一个:
GetTokenInformation
has an output parameter tokenInformation
which takes one of three values:
- TokenElevationTypeDefault = 1
- TokenElevationTypeFull = 2
- TokenElevationTypeLimited = 3
TokenElevantionTypeLimited的值表示用户与分裂标记在有限的权限运行。当高架TokenElevationTypeFull返回值。非管理员用户拥有TokenElevationTypeDefault的值。
A value of TokenElevantionTypeLimited indicates that the user is running with a split token with limited privileges. When elevated the TokenElevationTypeFull value is returned. Non-admin user has a value of TokenElevationTypeDefault.
有一个完整的code对C#的<一个例子href="http://www.davidmoore.info/2011/06/20/how-to-check-if-the-current-user-is-an-administrator-even-if-uac-is-on/">http://www.davidmoore.info/2011/06/20/how-to-check-if-the-current-user-is-an-administrator-even-if-uac-is-on/
There is a complete code example for C# at http://www.davidmoore.info/2011/06/20/how-to-check-if-the-current-user-is-an-administrator-even-if-uac-is-on/
这篇关于如何判断用户是管理员,即使不升高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!