【问题标题】:How to check if every users on the system has administrator rights in C#如何在 C# 中检查系统上的每个用户是否具有管理员权限
【发布时间】:2015-06-11 10:41:52
【问题描述】:

我有一个在我的系统中创建的用户列表:

  • 管理员(默认)
  • 客人
  • User1(标准用户)
  • User2(管理员用户)

我想知道通过 WMI 在 C# 中赋予所有这些用户的权限 ,这怎么可能?有没有其他方法可以找到它们。 即使一个用户有这个权利,它也必须退出循环

我使用下面的代码:

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (isAdmin == true)
{
    current_logged_user = "Yes";
}
else
{
    current_logged_user = "No";
}

这只会给我当前记录的信息,但我需要所有用户

link

下面的链接只是给管理员的成员 link

【问题讨论】:

    标签: c# wmi system-administration


    【解决方案1】:

    您应该能够通过 WMI 返回所有用户

            string groupNameToSearchFor = "Administrators"; // can be any group,maybe better to use something like builtin.administrators
    
            using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
            {
                ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
                ManagementObjectCollection users = usersSearcher.Get();
    
                foreach (ManagementObject user in users)
                {
                    if ((bool)user["LocalAccount"] == true && int.Parse(user["SIDType"].ToString()) == 1)
                    {
                        var userPrincipal = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, user["Name"].ToString());
                        GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, groupNameToSearchFor);
                        MessageBox.Show("Is User admin? -> " + (bool)userPrincipal.IsMemberOf(gp));
    
                    }
                }
            }
    

    你必须包括用于

    using System.DirectoryServices.AccountManagement;
    using System.Management;
    

    还要检查用户是否真的是用户而不是不同的对象(不确定我的检查是否足够)。


    编辑:您可以在获得列表后投射您需要的用户

            var localUsers = users.Cast<ManagementObject>().Where(
                u => (bool)u["LocalAccount"] == true &&
                     (bool)u["Disabled"] == false &&
                     (bool)u["Lockout"] == false &&
                     int.Parse(u["SIDType"].ToString()) == 1 &&
                     u["Name"].ToString() != "HomeGroupUser$");
    

    【讨论】:

    • 您的本地计算机。你也可以在活动目录中搜索,但你想要本地用户,对吧?
    • 当我使用“管理员”时出现异常??为什么??@Marc Wittmann
    • 我收到这样的错误:无法在此范围内声明名为“用户”的局部变量,因为它会给“用户”赋予不同的含义,该用户已在“父级或当前级”中使用' 表示其他东西的范围@Marc Wittmann
    • 这意味着您已经声明了一个变量 user.. 只需将您或我的用户变量重命名为 userToCheck 之类的其他名称。并确保您只使用循环中的真实用户(调试循环)
    • 相同的代码能否在每台不同的本地机器上工作,因为目前它在我的工作正常..@ Marc Wittmann
    【解决方案2】:

    你可以试试这个:

    bool IsInGroup(string user, string group)
    {
        using (var identity = new WindowsIdentity(user))
        {
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(group);
        }
    }
    

    您可以将 IsInRole(group) 更改为 IsInRole(WindowsBuiltInRole.Administrator)

    你有域服务器吗?

    【讨论】:

    • 我只想知道在我的单一系统@Fabian Stern 中创建的所有用户帐户都获得了哪些权限
    • 尽量避免使用名称为group的变量/参数。它是 LINQ 中使用的关键字。
    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多