2008-09-09 101 views

回答

2

我不知道关于.NET,但在Win32中,最简单的方法是调用IsUserAnAdmin()。如果您需要更多的控制,你可以打开进程令牌与CheckTokenMembership为每个需要检查

编辑组检查:pinvoke.net为.NET代码示例(感谢chopeen)

1

你可以循环组就像我在这个答案做:

Determining members of local groups via C#

阅读一些后,最简单的事情将是使用System.DirectoryServices.AccountManagement命名空间。下面是它如何被使用:

http://www.leastprivilege.com/SystemDirectoryServicesAccountManagement.aspx

样品:

public static bool IsUserInGroup(string username, string groupname, ContextType type) 
{ 
    PrincipalContext context = new PrincipalContext(type); 

    UserPrincipal user = UserPrincipal.FindByIdentity(
     context, 
     IdentityType.SamAccountName, 
     username); 
    GroupPrincipal group = GroupPrincipal.FindByIdentity(
     context, groupname); 

    return user.IsMemberOf(group); 
} 
+0

System.DirectoryServices.AccountManagement命名空间是.NET 3.5的新增功能,不是吗? – 2008-09-10 09:04:21

+0

链接文章中的第一句话: “通过一些新的3.5东西,我偶然发现了一个名为”System.DirectoryServices.AccountManagement“的新程序集 - 这引起了我的注意。” – Espo 2008-09-11 05:36:10

0

如果你谈论的是当前运行的用户则

using System.Security.Principal; 

WindowsIdentity identity = WindowsIdentity.GetCurrent(); 
WindowsPrincipal wp = new WindowsPrincipal(identity); 

if (wp.IsInRole("BUILTIN\Administrators")) 
    // Is Administrator 
else 
    // Is Not 

如果没有,那么我预计其可能身份设置为特定的用户,但没有研究如何。

相关问题