Ejemplo n.º 1
0
        public static List <string> GetLocalAdministratorsNames()
        {
            List <string>  admins       = new List <string>();
            DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
            string         adminsSID    = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).ToString();

            string localizedAdmin = new SecurityIdentifier(adminsSID).Translate(typeof(NTAccount)).ToString();

            localizedAdmin = localizedAdmin.Replace(@"BUILTIN\", "");

            DirectoryEntry admGroup     = localMachine.Children.Find(localizedAdmin, "group");
            object         adminmembers = admGroup.Invoke("members", null);

            //DirectoryEntry userGroup = localMachine.Children.Find("users", "group");
            ///object usermembers = userGroup.Invoke("members", null);

            //Retrieve each user name.
            foreach (object groupMember in (IEnumerable)adminmembers)
            {
                DirectoryEntry member = new DirectoryEntry(groupMember);

                string sidAsText = member.Path; /* GetTextualSID(member); */
                admins.Add(member.Name);
            }
            return(admins);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the name of the localized system admin  account.
        /// </summary>
        /// <returns>System.String.</returns>
        public static string GetLocalizedSystemAccountName()
        {
            string sid          = new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null).ToString();
            string localizedsid = new SecurityIdentifier(sid).Translate(typeof(NTAccount)).ToString();

            return(localizedsid.Replace(@"BUILTIN\", "").Replace("AUTORITE NT", ""));
        }
Ejemplo n.º 3
0
        public bool IsAdministratorsAccountName(string user, string machine)
        {
            //http://www.seirer.net/blog/2013/9/12/how-to-deal-with-localized-or-renamed-administrators-in-net
            DirectoryEntry localMachine = new DirectoryEntry($"WinNT://{machine}");
            string         adminsSID    = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).ToString();

            string localizedAdmin = new SecurityIdentifier(adminsSID).Translate(typeof(NTAccount)).ToString();

            localizedAdmin = localizedAdmin.Replace(@"BUILTIN\", "");

            DirectoryEntry admGroup = localMachine.Children.Find(localizedAdmin, "group");

            var adminmembers = (IEnumerable)admGroup.Invoke("members", null);

            var regex = new Regex(user.Replace("\\", "/"), RegexOptions.IgnoreCase);

            //Retrieve each user name.
            foreach (object groupMember in adminmembers)
            {
                DirectoryEntry member = new DirectoryEntry(groupMember);

                string sidAsText = member.Path ?? member.Username ?? member.Name;

                if (regex.IsMatch(sidAsText))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the name of the localized administrators groups account.
        /// </summary>
        /// <returns>System.String.</returns>
        public static string GetLocalizedAdministratorsGroupsAccountName()
        {
            string adminsSid      = new SecurityIdentifier("S-1-5-32-544").ToString();
            string localizedAdmin = new SecurityIdentifier(adminsSid).Translate(typeof(NTAccount)).ToString();

            localizedAdmin = localizedAdmin.Replace(@"BUILTIN\", "");
            return(localizedAdmin);
        }