Search code examples
asp.net-web-apimicrosoft-graph-apiasp.net-identityclaims-based-identitymicrosoft-graph-sdks

Calling Graph Api from Web Api doesn't return all user data


In my ASP.NET Core Web Api, I'm trying to call Graph API to retrieve the data of other users in the organization. I'm using the On Behalf Of flow. The bootstrap token is passed in from SPA client code.

In the Startup code, I have:

builder.Services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph("https://graph.microsoft.com/beta", "user.read user.readbasic.all profile")
    .AddInMemoryTokenCaches();

In the Controller's Action, I have:

_graphServiceClient.Users.Request()
            .Filter($"mail eq 'someuser@myorg.com'")
            .Select((user) => new {
                DisplayName = user.DisplayName,
                City = user.City,
                Country = user.Country,
                BusinessPhones = user.BusinessPhones
            })
            .Request()
            .GetAsync();

However, at runtime, I only get the DisplayName value. I don't get the values of City, Country or BusinessPhones. They are al null.

When I tried the same query in Graph Explorer, I saw the values for all of them.

What am I missing?


Solution

  • First, your code snippet has an error that you wrote 2 .Request(), you should remove one.

    enter image description here

    Whatever flow you used are all generating an access token so that you can access the api, therefore when you got correct data from the api, that means you have a right setting in using the flow, so I think if there's any issue, it should locate at the part of calling api. Then I did some test in my side.

    Could you pls check if you call the api directly, you can get the value of City, Country and BUsinessPhones these 3 properties? Per my test, when I call the api, I can get response like screenshot below and by default it's null

    https://graph.microsoft.com/beta/users?$filter=mail eq 'tinywang@xxx.onmicrosoft.com'&$select=mail,city,country,DisplayName,BusinessPhones,userType,usageLocation

    enter image description here

    And when I followed this tutorial to call the api via sdk, I got the same result:

    enter image description here

    My idea is checking the response of calling the api first and if we can't find the issue, then could you pls provide the tutorial you followed to help reproduce the issue.