crlic306
crlic306

Reputation: 35

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.

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.

        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");

Upvotes: 0

Views: 843

Answers (2)

crlic306
crlic306

Reputation: 35

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 (!(userList.Contains(whateverusernameyouwanttosearch)))

Upvotes: 0

J T
J T

Reputation: 71

If the program is compiling without any issues then it may be that the event handler is not setup for Form1_Load().

You may have to add to the Form1.Designer.cs file something like:

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

Let me know if this helps.

Upvotes: 0

Related Questions