Example #1
0
        public BaseActiveDirectoryGroup GetGroup(string groupIdentity, bool loadSubProperties)
        {
            var adGroup = new BaseActiveDirectoryGroup();

            foreach (var pc in _principalContexts)
            {
                var groupPrincipal = GroupPrincipal.FindByIdentity(pc, groupIdentity);

                if (groupPrincipal != null)
                {
                    adGroup = MapGroupPrincipalToGroup(groupPrincipal, loadSubProperties);
                    break;
                }
            }

            return(adGroup);
        }
Example #2
0
        /// <summary>
        /// Maps a given group principal to a new group object
        /// </summary>
        private BaseActiveDirectoryGroup MapGroupPrincipalToGroup(GroupPrincipal groupPrincipal, bool loadSubProperties)
        {
            var adGroup = new BaseActiveDirectoryGroup
            {
                Id = ConvertSidToString(groupPrincipal.Sid),
                NameOrDescription = groupPrincipal.Name,
            };

            //Check to load subproperties
            if (loadSubProperties)
            {
                //Load groups additional data
                var directoryEntry = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();

                adGroup.Path         = directoryEntry.Path;
                adGroup.CreationDate = (DateTime?)directoryEntry.InvokeGet(whenCreatedKey);
                adGroup.Owner        = (string)directoryEntry.InvokeGet(managedByKey);

                if (!string.IsNullOrEmpty(adGroup.Owner))
                {
                    adGroup.Owner = adGroup.Owner.Substring(3, (adGroup.Owner.IndexOf("OU=") - 3)).RemoveSpecialChars();
                }

                //Load group users
                var groupUsersList = groupPrincipal.GetMembers()
                                     .Where(m => m.GetType() == typeof(UserPrincipal))
                                     .Cast <UserPrincipal>()
                                     .Select(gul => new BaseIdentification
                {
                    Id = ConvertSidToString(gul.Sid),
                    NameOrDescription = gul.SamAccountName
                });

                //Add users inside current AD group
                adGroup.Users.AddRange(groupUsersList);
            }

            return(adGroup);
        }