Beispiel #1
0
        private static GroupMemberships ExpandVSTSGroup(GraphHttpClient graphClient, GraphGroup group)
        {
            GroupMemberships groupMemberships = new GroupMemberships();

            // Convert all memberships into GraphSubjectLookupKeys
            List <GraphSubjectLookupKey> lookupKeys  = new List <GraphSubjectLookupKey>();
            List <GraphMembership>       memberships = graphClient.GetMembershipsAsync(group.Descriptor, Microsoft.VisualStudio.Services.Graph.GraphTraversalDirection.Down).Result;

            foreach (var membership in memberships)
            {
                lookupKeys.Add(new GraphSubjectLookupKey(membership.MemberDescriptor));
            }
            IReadOnlyDictionary <SubjectDescriptor, GraphSubject> subjectLookups = graphClient.LookupSubjectsAsync(new GraphSubjectLookup(lookupKeys)).Result;

            foreach (GraphSubject subject in subjectLookups.Values)
            {
                switch (subject.Descriptor.SubjectType)
                {
                //member is an AAD user
                case Constants.SubjectType.AadUser:
                    groupMemberships.AddUser((GraphUser)subject);
                    break;

                //member is an MSA user
                case Constants.SubjectType.MsaUser:
                    groupMemberships.AddUser((GraphUser)subject);
                    break;

                //member is a nested AAD group
                case Constants.SubjectType.AadGroup:
                    groupMemberships.AddAADGroup((GraphGroup)subject);
                    break;

                //member is a nested VSTS group
                case Constants.SubjectType.VstsGroup:
                    GroupMemberships subGroupMemberships = ExpandVSTSGroup(graphClient, (GraphGroup)subject);
                    groupMemberships.Add(subGroupMemberships);
                    break;

                default:
                    throw new Exception("Unknown SubjectType: " + subject.Descriptor.SubjectType);
                }
            }

            return(groupMemberships);
        }
Beispiel #2
0
        public List <string> RunEnumerateMembersOfGroupsUsingClientLib(string groupDisplayName)
        {
            Uri uri = new Uri(_uri);
            AuthenticationContext ctx            = GetAuthenticationContext(null);
            AuthenticationResult  vstsAuthResult = ctx.AcquireTokenAsync(VSTSResourceId, _clientId.ToString(), _replyUrl, new PlatformParameters(PromptBehavior.Always)).Result;
            VssConnection         vssConnection  = new VssConnection(new Uri(_uri), new VssOAuthAccessTokenCredential(vstsAuthResult.AccessToken));

            using (GraphHttpClient graphClient = vssConnection.GetClient <GraphHttpClient>())
            {
                // Get the VSTS group
                GraphGroup group = GetVSTSGroupByDisplayName(graphClient, groupDisplayName);

                // Expand membership of the VSTS group to users and AAD Groups
                GroupMemberships groupMemberships = ExpandVSTSGroup(graphClient, group);

                List <string> expandedUsers = new List <string>();
                foreach (GraphUser user in groupMemberships.Users)
                {
                    expandedUsers.Add(user.PrincipalName);
                }

                //exchange VSTS token for Microsoft graph token
                AuthenticationResult graphAuthResult = ctx.AcquireTokenAsync(GraphResourceId, _clientId.ToString(), _replyUrl, new PlatformParameters(PromptBehavior.Auto)).Result;

                // Resolve all AAD Groups to users using Microsoft graph
                foreach (GraphGroup AADGroup in groupMemberships.AADGroups)
                {
                    List <AadGroupMember> aadGroupUsers = ExpandAadGroups(graphAuthResult.AccessToken, AADGroup);
                    foreach (AadGroupMember aadGroupUser in aadGroupUsers)
                    {
                        expandedUsers.Add(aadGroupUser.userPrincipalName);
                    }
                }

                return(expandedUsers);
            }
        }
Beispiel #3
0
 public void Add(GroupMemberships memberships)
 {
     this.Users.AddRange(memberships.Users);
     this.AADGroups.AddRange(memberships.AADGroups);
 }