我怎样才能知道我的过程中以管理员身份运行? [英] How can I tell if my process is running as Administrator?

查看:266
本文介绍了我怎样才能知道我的过程中以管理员身份运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一些额外的UI元素的时候,而不是当它不是,类似的工作室在其标题栏中显示2008年管理员怎么视觉时,作为管理员运行的进程正在以管理员身份运行。我怎么知道?

I would like to display some extra UI elements when the process is being run as Administrator as opposed to when it isn't, similar to how Visual Studio 2008 displays 'Administrator' in its title bar when running as admin. How can I tell?

推荐答案

从技术上讲,如果你想看看如果成员是本地管理员的帐户的,那么你就可以得到的security标识符(SID)的当前用户通过<一href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.user.aspx"><$c$c>User 的WindowsIdentity 属性,像这样(静态 GetCurrent 方法获取当前Windows用户):

Technically, if you want to see if the member is the local administrator account, then you can get the security identifier (SID) of the current user through the User property on the WindowsIdentity class, like so (the static GetCurrent method gets the current Windows user):

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

string sid = windowsIdentity.User.ToString();

用户属性返回其有许多的用户的SID $ P $各种组和用户 pdefined值。

The User property returns the SID of the user which has a number of predefined values for various groups and users.

然后你会检查,看<一href="http://blogs.technet.com/b/heyscriptingguy/archive/2005/07/22/how-can-i-determine-if-the-local-administrator-account-has-been-renamed-on-a-computer.aspx">the SID有以下模式,表明它是本地管理员帐户(这是一个众所周知的SID):

S-1-5 - {其他SID部分} -500

S-1-5-{other SID parts}-500

或者,如果你不想来解析字符串,你可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.security.principal.securityidentifier.aspx"><$c$c>SecurityIdentifier类:

Or, if you don't want to parse strings, you can use the SecurityIdentifier class:

// Get the built-in administrator account.
var sid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, 
    null);

// Compare to the current user.
bool isBuiltInAdmin = (windowsIdentity.User == sid);

不过,我怀疑你的真正的想知道的是,如果当前用户是Administrators组的成员的集团的本地机器。您可以使用得到这个SID的<一个href="http://msdn.microsoft.com/en-us/library/system.security.principal.wellknownsidtype.aspx"><$c$c>WellKnownSidType BuiltinAdministratorsSid

However, I suspect that what you really want to know is if the current user is a member of the administrators group for the local machine. You can get this SID using the WellKnownSidType of BuiltinAdministratorsSid:

// Get the SID of the admin group on the local machine.
var localAdminGroupSid = new SecurityIdentifier(
    WellKnownSidType.BuiltinAdministratorsSid, null);

然后你可以检查<一href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.groups.aspx"><$c$c>Groups的WindowsIdentity 的用户属性,查看该用户是本地管理员组的成员,像这样:

Then you can check the Groups property on the WindowsIdentity of the user to see if that user is a member of the local admin group, like so:

bool isLocalAdmin = windowsIdentity.Groups.
    Select(g => (SecurityIdentifier) g.Translate(typeof(SecurityIdentifier))).
    Any(s => s == localAdminGroupSid);

这篇关于我怎样才能知道我的过程中以管理员身份运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆