简体   繁体   English

查找指定的用户(字符串)是否是本地管理员组的成员

[英]Find if specified user (string) is a member of local Administrators group

I am trying to find if a user I identify is a member of the local Administrators group. 我正在尝试确定我确定的用户是否是本地Administrators组的成员。

But my code does nothing... 但是我的代码什么都不做...

Please see me code below. 请在下面查看我的代码。

Also, this is being executed in my public void Form1_Load(object sender, EventArgs e) {} so it is done every time at application start up. 另外,这是在我的public void Form1_Load(object sender, EventArgs e) {}因此每次在应用程序启动时都会执行此操作。

        string localUser = WindowsIdentity.GetCurrent().Name.ToString();
        char[] trimmingsEnd = { 'a', 'd', 'm', 'i', 'n' };
        string trimmedlocalEnd = localUser.TrimEnd(trimmingsEnd);
        char[] trimmingsFront = { 'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', '\\' };
        string trimmedlocalUser = trimmedlocalEnd.TrimStart(trimmingsFront);

        WindowsIdentity windowsIdentity = new WindowsIdentity(trimmedlocalUser);
        WindowsPrincipal principal = new WindowsPrincipal(windowsIdentity);
        bool IsAdmin = principal.IsInRole("BUILTIN\\" + "Administrators");
             if (IsAdmin == false)
                 MessageBox.Show("not part of admin");
             if (IsAdmin == true)
                 MessageBox.Show("part of admin");

If the program is compiling without any issues then it may be that the event handler is not setup for Form1_Load(). 如果程序正在编译而没有任何问题,则可能是未为Form1_Load()设置事件处理程序。

You may have to add to the Form1.Designer.cs file something like: 您可能需要向Form1.Designer.cs文件中添加以下内容:

this.Load += new System.EventHandler(Form1_Load);

Let me know if this helps. 让我知道是否有帮助。

So, I ditched the method above, as all I could find is the current user...but I needed to search for two user names in the local administrators group. 因此,我放弃了上面的方法,因为我只能找到当前用户...但是我需要在本地管理员组中搜索两个用户名。

The following code worked perfectly for what I needed! 以下代码可完美满足我的需求! Hope this helps someone. 希望这对某人有帮助。

//Get all users from the local Administrators group and create list
            DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
            DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
            object members = admGroup.Invoke("members", null);
            List<string> userList = new List<string>();
//Get current user
            string localUser1 = WindowsIdentity.GetCurrent().Name.ToString();
//Take domain name off
            char[] trimmingsFront = { 'D', 'O', 'M', 'A', 'I', 'N', '\\' };
            string trimmedlocalFront = localUser1.TrimStart(trimmingsFront);
//Take "admin" off username
            char[] trimmingsEnd = { 'a', 'd', 'm', 'i', 'n' };
            string trimmedlocalUser = trimmedlocalFront.TrimEnd(trimmingsEnd);
//Add each local Administrator to list
            foreach (object groupMember in (IEnumerable)members)
            {
                DirectoryEntry member = new DirectoryEntry(groupMember);
                userList.Add(member.Name);
            }
//Check if users are not part of list
            if (!(userList.Contains(trimmedlocalFront)))
                MessageBox.Show(trimmedlocalFront + " is not a member of the local Administrators group. The drag and drop functionality will not work unless " + trimmedlocalFront + " is a member of the local Administrators group. After " + trimmedlocalFront + " is added, please restart your machine for the changes to take effect.", "Local Administrator Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else if (!(userList.Contains(trimmedlocalUser)))
                MessageBox.Show(trimmedlocalUser + " is not a member of the local Administrators group. The drag and drop functionality will not work unless " + trimmedlocalUser + " is a member of the local Administrators group. After " + trimmedlocalUser + " is added, please restart your machine for the changes to take effect.", "Local Administrator Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);

If you want to check for your own user names, for the 'if', do: 如果要检查自己的用户名,请为“ if”检查:

if (!(userList.Contains(whateverusernameyouwanttosearch)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 识别用户是否在本地管理员组中 - Identifying if a user is in the local administrators group 将组添加到本地管理员 - Adding group to local administrators 如何测试AD用户是否是本地组的成员 - How to test if AD User is a member of a local group 如何获取来宾/管理员的本地组名称? - How can I get the local group name for guests/administrators? Integrated Security = SSPI是否需要Administrators组中的Windows用户? - Does Integrated Security=SSPI require a Windows user in the Administrators group? 检查用户是否是组的成员 - check if user is a member of a group 使用c#和WMI在远程计算机上的本地管理员组中添加和删除域用户 - Add and remove domain users from local administrators group on remote computers using c# and WMI 为什么本地管理员的主要组显示为域用户 {S-1-5-21-<domain> -513}</domain> - Why the primary group of Local Administrators appears as Domain Users {S-1-5-21-<domain>-513} HttpContext.User.Identity 中缺少组内置管理员(SID:S-1-5-32-544) - Missing Group Built-in Administrators (SID: S-1-5-32-544) in HttpContext.User.Identity 使用C#和AccountManagment命名空间从远程计算机上的管理员组中删除用户帐户 - Remove user account from administrators group on remote machine using C# and AccountManagment namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM