Feedback

C# - Ermittelt Remote, ob der User Admin, PowerUser oder User -Re

Veröffentlicht von am 4/12/2007
(25 Bewertungen)
Ermittelt Remote, ob der User Admin, PowerUser oder User -Rechte hat

using System.Collections.Generic;
using System.Management;
using System.Security.Principal;

Läuft ab .NET 2.0
/// <summary>
        /// Ermittelt Remote, ob der User Admin, PowerUser oder User -Rechte hat
        /// </summary>
        /// <param name="ComputerName"></param>
        /// <param name="UserName"></param>
        /// <returns>local GroupName</returns>
        public static string GetLocalUserRights(string ComputerName, string UserName)
        {
            Dictionary<string, string> LocalRights = new Dictionary<string,string>();
            ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Group WHERE LocalAccount = TRUE");
            ManagementScope ms = new ManagementScope("\\\\" + ComputerName + "\\root\\cimv2");
            mos.Scope = ms;
            try
            {
                mos.Scope.Connect();
            }
            catch (Exception)
            {
                return null;
            }
            SecurityIdentifier sidLocalAdmins = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
            SecurityIdentifier sidLocalPowerUser = new SecurityIdentifier(WellKnownSidType.BuiltinPowerUsersSid, null);
            SecurityIdentifier sidLocalUser = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);

            foreach (ManagementObject mo in mos.Get())
            {
                if (mo["SID"].ToString() == sidLocalAdmins.Value | mo["SID"].ToString() == sidLocalPowerUser.Value | mo["SID"].ToString() == sidLocalUser.Value)
                {
                    ManagementObjectSearcher userSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_GroupUser Where GroupComponent = \"Win32_Group.Domain=\'" + ComputerName + "\',Name=\'" + mo["Name"] + "\'\"");
                    userSearcher.Scope = ms;
                    try
                    {
                        userSearcher.Scope.Connect();
                        foreach (ManagementObject userObject in userSearcher.Get())
                        {
                            string UName = userObject["PartComponent"].ToString();
                            UName = UName.Substring(UName.IndexOf("=") + 1);
                            UName = UName.Substring(UName.IndexOf("=") + 1).Replace("\"", "");
                            if (UName.IndexOf(UserName, 0, StringComparison.OrdinalIgnoreCase) == 0)
                                LocalRights.Add(mo["SID"].ToString(), mo["Name"].ToString());
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            if (LocalRights.ContainsKey(sidLocalAdmins.Value)) return LocalRights[sidLocalAdmins.Value];
            if (LocalRights.ContainsKey(sidLocalPowerUser.Value)) return LocalRights[sidLocalPowerUser.Value];
            if (LocalRights.ContainsKey(sidLocalUser.Value)) return LocalRights[sidLocalUser.Value];
            string UserAccount = sidLocalUser.Translate(typeof(NTAccount)).Value;
            return UserAccount.Substring(UserAccount.IndexOf("\\") + 1);
        }
Abgelegt unter WMI, Account.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!