Exemple #1
0
        //public Task<LdapGroupCollection> GetGroupsAssignedToUserAsync(LdapUserInfo user)
        //{
        //    return Task<LdapGroupCollection>.Run(() => GetGroupsAssignedToUser(user));
        //}

        /// <summary>
        /// Recursive method to retrieve the parent groups of the spcified group.
        /// </summary>
        /// <param name="group">The child group of the parent groups to be found.</param>
        /// <param name="groups">A collection of all of the groups that have already been added to the list.</param>
        protected void LoadParentsOfGroup(LdapGroupInfo group, LdapGroupCollection groups)
        {
            if (group.MemberOf.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (var parentDN in group.MemberOf)
                {
                    sb.Append($@"(distinguishedName={parentDN})");
                }
                if (group.MemberOf.Count > 1)
                {
                    sb.Insert(0, "(&(objectClass=group)(|");
                    sb.Append("))");
                }
                else
                {
                    sb.Insert(0, "(&(objectClass=group)");
                    sb.Append(")");
                }
                string filter = sb.ToString();
                SearchResultCollection resList = GetSearchResults(GetRootEntry(), filter, searchSubtrees: true);
                foreach (SearchResult groupRes in resList)
                {
                    var parentGroup = groupRes.GetDirectoryEntry().CopyTo(new LdapGroupInfo());
                    if (groups.IndexOfDN(parentGroup.DistinguishedName) == -1)
                    {
                        groups.Add(parentGroup);
                        LoadParentsOfGroup(parentGroup, groups);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Retrieves a list of user accounts that are assigned to the specified group and/or the children of that group.
        /// note: The the Groups list for each found user will not be populated.  You will need to call the LoadGroupsAssignedToUser method to populate this list.
        /// </summary>
        public LdapUserCollection GetUsersInGroup(LdapGroupInfo group)
        {
            var groups = new LdapGroupCollection();

            groups.Add(group);
            return(GetUsersInGroups(groups));
        }
Exemple #3
0
        /// <summary>
        /// Retrieves the specified groups from the LDAP server.
        /// </summary>
        /// <param name="guid">The GUID of the the group to be retrieved.</param>
        public LdapGroupCollection GetGroupsByGuids(List <Guid> guids)
        {
            var groups = new LdapGroupCollection();

            if (guids.Count == 0)
            {
                return(groups);
            }
            var sb = new StringBuilder();

            sb.Append("(&(objectClass=group)");
            if (guids.Count > 1)
            {
                sb.Append("(|");
            }
            foreach (var guid in guids)
            {
                sb.Append($@"(objectGUID={ConvertGuidToOctetString(guid)})");
            }
            if (guids.Count > 1)
            {
                sb.Append(")");
            }
            sb.Append(")");
            string filter = sb.ToString();
            SearchResultCollection resList = GetSearchResults(GetRootEntry(), filter, searchSubtrees: true);

            foreach (SearchResult res in resList)
            {
                var entry = res.GetDirectoryEntry();
                var group = entry.CopyTo(new LdapGroupInfo());
                groups.Add(group);
            }
            return(groups);
        }
Exemple #4
0
        /// <summary>
        /// Recursive method to retrieve the child groups of the spcified group.
        /// </summary>
        /// <param name="group">The parent group containing the children groups to be found.</param>
        /// <param name="groups">A collection of all of the groups that have already been added to the list.</param>
        protected void LoadChildrenOfGroup(LdapGroupInfo group, LdapGroupCollection groups)
        {
            string filter = $"(&(objectClass=group)(memberOf={group.DistinguishedName}))";
            SearchResultCollection resList = GetSearchResults(GetRootEntry(), filter, searchSubtrees: true);

            foreach (SearchResult groupRes in resList)
            {
                var childGroup = groupRes.GetDirectoryEntry().CopyTo(new LdapGroupInfo());
                if (groups.IndexOfDN(childGroup.DistinguishedName) == -1)
                {
                    groups.Add(childGroup);
                    LoadChildrenOfGroup(childGroup, groups);
                }
            }
        }
Exemple #5
0
 public LdapUserInfo(LdapUserInfo entry) : base(entry)
 {
     LoginName         = entry.LoginName;
     UserPrincipalName = entry.UserPrincipalName;
     Email             = entry.Email;
     FirstName         = entry.FirstName;
     MiddleName        = entry.MiddleName;
     LastName          = entry.LastName;
     PrimaryGroupSID   = entry.PrimaryGroupSID;
     LockedOut         = entry.LockedOut;
     Disabled          = entry.Disabled;
     Groups            = new LdapGroupCollection();
     foreach (LdapGroupInfo group in entry.Groups)
     {
         Groups.Add(new LdapGroupInfo(group));
     }
 }
Exemple #6
0
        /// <summary>
        /// Returns a list of the groups in the specified folder (ogranizationalUnit).
        /// </summary>
        /// <param name="folderDN">The distinctive name of the folder holding the groups to be returned.
        /// <param name="includeSubFlders">Set to true to include subfolds in the search.
        public LdapGroupCollection GetGroupsInFolder(string folderDN, bool includeSubFolders = false)
        {
            var            groups    = new LdapGroupCollection();
            string         filter    = "(objectClass=group)";
            DirectoryEntry baseEntry = GetEntryByDN(folderDN);

            if (baseEntry == null)
            {
                throw new NullReferenceException("Unable to find LDAP organizational unit with DN=" + folderDN ?? "");
            }
            SearchResultCollection resList = GetSearchResults(baseEntry, filter, searchSubtrees: includeSubFolders);

            foreach (SearchResult res in resList)
            {
                var entry = res.GetDirectoryEntry();
                var group = entry.CopyTo(new LdapGroupInfo());
                groups.Add(group);
            }
            return(groups);
        }
Exemple #7
0
        /// <summary>
        /// Retrieves a list of user accounts that are assigned to the specified list of groups and/or the children of those groups.
        /// note: The the Groups list for each found user will not be populated.  You will need to call the LoadGroupsAssignedToUser method to populate this list.
        /// </summary>
        public LdapUserCollection GetUsersInGroups(LdapGroupCollection groups)
        {
            var users = new LdapUserCollection();

            if (groups.Count == 0)
            {
                return(users);
            }

            var childGroups = new LdapGroupCollection();

            childGroups.AddRange(groups);
            foreach (var childGroup in groups)
            {
                LoadChildrenOfGroup(childGroup, childGroups);
            }

            var sb          = new StringBuilder();
            var dnList      = new List <string>();
            var groupIdList = new List <string>();

            foreach (var group in childGroups)
            {
                if (!dnList.Contains(group.DistinguishedName))
                {
                    dnList.Add(group.DistinguishedName);
                    sb.Append($@"(memberOf={group.DistinguishedName})");
                }
                int dashPos = group.SID.LastIndexOf("-");
                if (dashPos > -1)
                {
                    string primaryGroupID = group.SID.Substring(dashPos + 1);
                    if (!groupIdList.Contains(primaryGroupID))
                    {
                        groupIdList.Add(primaryGroupID);
                        sb.Append($@"(primaryGroupID={primaryGroupID})");
                    }
                }
            }
            if ((dnList.Count + groupIdList.Count) > 1)
            {
                sb.Insert(0, "(&(objectCategory=person)(objectClass=user)(|");
                sb.Append("))");
            }
            else
            {
                sb.Insert(0, "(&(objectCategory=person)(objectClass=user)");
                sb.Append(")");
            }
            string filter = sb.ToString();
            SearchResultCollection resList = GetSearchResults(GetRootEntry(), filter, searchSubtrees: true);

            foreach (SearchResult userRes in resList)
            {
                var entry = userRes.GetDirectoryEntry();
                var user  = entry.CopyTo(new LdapUserInfo());
                if (users.IndexOfDN(user.DistinguishedName) == -1)
                {
                    users.Add(user);
                }
            }
            return(users);
        }
Exemple #8
0
 public LdapUserInfo() : base()
 {
     Groups = new LdapGroupCollection();
 }