Exemplo n.º 1
0
        private IEnumerable <string> GetAllGroupsFromUser(string userName, AccountSearchReturnType returnType)
        {
            var computerName = GetComputerSystemFromTarget();

            var wqlToGetGroupsFromUser = BuildWqlToGetGroupsFromUser(computerName, userName);
            var wqlResult = this.WmiProvider.ExecuteWQL(wqlToGetGroupsFromUser);

            if (wqlResult.IsEmpty())
            {
                return(new string[0]);
            }

            var groupComponents = wqlResult.Select(gu => gu.GetFieldValueAsString("GroupComponent")).ToArray();

            var groupInfos = groupComponents
                             .Select(gc => ExtractGroupFromGroupPart(gc))
                             .Where(g => g != null)
                             .ToArray();

            if (returnType == AccountSearchReturnType.Name)
            {
                return(groupInfos.Select(g => g.Item2));
            }

            var allUserGroups = new List <string>();

            foreach (var groupInfo in groupInfos)
            {
                var group = GetGroup(groupInfo.Item1, groupInfo.Item2);
                allUserGroups.Add(group.GetFieldValueAsString("SID"));
            }

            return(allUserGroups);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the groups the user belongs
        /// </summary>
        /// <param name="username">The user name in FQDN format. For SYSTEM accounts do not use FQDN.</param>
        /// <param name="returnType">Defines wether will be returned the group name ou SID.</param>
        /// <returns>A list of group names or group SIDs according with returnType argument.</returns>
        public virtual IEnumerable <String> GetUserGroups(string username, AccountSearchReturnType returnType)
        {
            var address          = this.TargetInfo.GetAddress();
            var credentials      = this.TargetInfo.credentials;
            var principalContext = AccManUtils.accManConnect(address, credentials.GetUserName(), credentials.GetPassword());

            Principal user            = null;
            var       isSystemAccount = !username.Contains("\\");

            if (isSystemAccount)
            {
                var wqlGetUserSID = GetWqlToGetUserDetails(username);
                var wqlResult     = this.WmiProvider.ExecuteWQL(wqlGetUserSID).FirstOrDefault();
                if (wqlResult != null)
                {
                    var userSID = wqlResult.GetFieldValueAsString("SID");
                    user = Principal.FindByIdentity(principalContext, IdentityType.Sid, userSID);
                }
            }
            else
            {
                user = Principal.FindByIdentity(principalContext, IdentityType.SamAccountName, username);
                if (user == null)
                {
                    user = Principal.FindByIdentity(principalContext, IdentityType.Name, username);
                }
            }

            var groups = new List <String>();

            if (user == null)
            {
                return(groups);
            }

            var foundGroups = user.GetGroups();

            if (foundGroups == null)
            {
                return(groups);
            }

            if (returnType.Equals(AccountSearchReturnType.SID))
            {
                return(foundGroups.Select(g => g.Sid.Value));
            }

            return(foundGroups.Select(g => g.Name));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the groups the user belongs
        /// </summary>
        /// <param name="username">The user name in FQDN format. For SYSTEM accounts do not use FQDN.</param>
        /// <param name="returnType">Defines wether will be returned the group name ou SID.</param>
        /// <returns>A list of group names or group SIDs according with returnType argument.</returns>
        public virtual IEnumerable <String> GetUserGroups(string username, AccountSearchReturnType returnType)
        {
            var address     = this.TargetInfo.GetAddress();
            var credentials = this.TargetInfo.credentials;

            Principal user            = null;
            var       isSystemAccount = !username.Contains("\\");

            if (isSystemAccount)
            {
                return(GetAllGroupsFromUser(username, returnType));
            }
            else
            {
                var principalContext = AccManUtils.accManConnect(address, credentials.GetUserName(), credentials.GetPassword());
                user = Principal.FindByIdentity(principalContext, IdentityType.SamAccountName, username);
                if (user == null)
                {
                    user = Principal.FindByIdentity(principalContext, IdentityType.Name, username);
                }
            }

            var groups = new List <String>();

            if (user == null)
            {
                return(groups);
            }

            var foundGroups = user.GetGroups();

            if (foundGroups == null)
            {
                return(groups);
            }

            if (returnType.Equals(AccountSearchReturnType.SID))
            {
                return(foundGroups.Select(g => g.Sid.Value));
            }

            return(foundGroups.Select(g => g.Name));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the groups the user belongs 
        /// </summary>
        /// <param name="username">The user name in FQDN format. For SYSTEM accounts do not use FQDN.</param>
        /// <param name="returnType">Defines wether will be returned the group name ou SID.</param>
        /// <returns>A list of group names or group SIDs according with returnType argument.</returns>
        public virtual IEnumerable<String> GetUserGroups(string username, AccountSearchReturnType returnType)
        {
            var address = this.TargetInfo.GetAddress();
            var credentials = this.TargetInfo.credentials;
            var principalContext = AccManUtils.accManConnect(address, credentials.GetUserName(), credentials.GetPassword());

            Principal user = null;
            var isSystemAccount = !username.Contains("\\");

            if (isSystemAccount)
            {
                var wqlGetUserSID = GetWqlToGetUserDetails(username);
                var wqlResult = this.WmiProvider.ExecuteWQL(wqlGetUserSID).FirstOrDefault();
                if (wqlResult != null)
                {
                    var userSID = wqlResult.GetFieldValueAsString("SID");
                    user = Principal.FindByIdentity(principalContext, IdentityType.Sid, userSID);
                }
            }
            else
            {
                user = Principal.FindByIdentity(principalContext, IdentityType.SamAccountName, username);
                if (user == null)
                    user = Principal.FindByIdentity(principalContext, IdentityType.Name, username);
            }

            var groups = new List<String>();
            if (user == null)
                return groups;

            var foundGroups = user.GetGroups();
            if (foundGroups == null)
                return groups;

            if (returnType.Equals(AccountSearchReturnType.SID))
                return foundGroups.Select(g => g.Sid.Value);

            return foundGroups.Select(g => g.Name);
        }