1

I am using the GraphHttpClient class to try and retrieve all groups that a user is a member of in Visual Studio Team Services. I can connect fully to VSTS and the following code runs, but I often get errors with the VSTS built-in accounts. I am not sure what I am doing wrong or how to correct this so that the code runs without exception.

What is the best way to loop all the users and produce a list of the groups they are a member of?

GraphHttpClient graphClient = connection.GetClient<GraphHttpClient>();
PagedGraphUsers users = graphClient.ListUsersAsync().Result;

foreach (var user in users.GraphUsers)
{
    if (user.Origin.Equals("aad"))
    {                    
        var ent = graphClient.ListMembershipsAsync(user.Descriptor).Result.ToList();
        foreach (var graphMembership in ent)
        {
            var group = graphClient.GetGroupAsync(graphMembership.ContainerDescriptor).Result;

            Console.Out.WriteLine("{0},{1}",
                user.PrincipalName.PadRight(20),
                group.PrincipalName);
        }
    }
    else
    {
        Console.Out.WriteLine("{0},Service Accounts",
            user.DisplayName.PadRight(20));
    }
}

When I remove the IF statement and let all users go through the ListMememberhships, I get this error:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Microsoft.VisualStudio.Services.Graph.InvalidSubjectTypeException: acs``
Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Paul Duer
  • 1,100
  • 1
  • 13
  • 32

1 Answers1

1

The Descriptor start with acs are service account, such as Hosted VS2017 agent service, TeamFoundationService.

So you can change condition like this:

if (!user.Descriptor.ToString().StartsWith("acs"))
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53