public bool IsMemberOfGroup(string groupName)
            {
                if (groupName == null)
                {
                    throw new ArgumentNullException(nameof(groupName));
                }

                var userSearchResult = this.directory.TryGetPrincipal(PrincipalSearchType.Users, this.userId.ToFullyQualifiedName());

                if (userSearchResult == null)
                {
                    return(false);
                }

                var groupSet    = LDAP.ExtractGroupNames(userSearchResult);
                var compareName = GroupId.Parse(groupName)?.Principal ?? groupName;

                if (groupSet.Contains(compareName))
                {
                    return(true);
                }

                if (this.directory.SearchGroupsRecursively)
                {
                    var groupsToSearch = new Queue <string>(groupSet);
                    var groupsSearched = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                    while (groupsToSearch.Count > 0)
                    {
                        var nextGroup = groupsToSearch.Dequeue();
                        if (groupsSearched.Add(nextGroup))
                        {
                            var groupSearchResult = this.directory.TryGetPrincipal(PrincipalSearchType.Groups, nextGroup);
                            if (groupSearchResult != null)
                            {
                                var groupGroups = LDAP.ExtractGroupNames(groupSearchResult);
                                foreach (var g in groupGroups)
                                {
                                    groupsToSearch.Enqueue(g);
                                }
                            }
                        }
                    }

                    return(groupsSearched.Contains(compareName));
                }

                return(false);
            }
示例#2
0
            public bool IsMemberOfGroup(string groupName)
            {
                if (groupName == null)
                {
                    throw new ArgumentNullException(nameof(groupName));
                }

                var userSearchResult = directory.TryGetPrincipal(PrincipalSearchType.Users, this.userId.ToFullyQualifiedName());

                if (userSearchResult == null)
                {
                    return(false);
                }

                var groupSet = LDAP.ExtractGroupNames(userSearchResult);

                return(groupSet.Contains(GroupId.Parse(groupName)?.Principal ?? groupName));
            }