Search code examples
c#asp.netasp.net-core-mvcmicrosoft-graph-api

_graphServiceClient.Me.GetAsync() throws IUserRequestBuilder does not contain GetAsync()


I want to obtain data from GraphServiceClient according to this example

However, the line

var user = await _graphServiceClient.Me.GetAsync();

throws the following error:

IUserRequestBuilder does not contain a definition for GetAsync

I've installed the following NuGet packages:

  • Microsoft.Identity.Web 2.17.5
  • Microsoft.Identity.Web.MicrosoftGraph 2.17.5
  • Microsoft.Graph 4.36.0

The di is set up like this

string[] initialScopes = Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' ');

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureB2C"))
    .EnableTokenAcquisitionToCallDownstreamApi(options =>
        {
            var section = Configuration.GetSection("AzureAd");
            options.RedirectUri = section.GetValue<string>("RedirectUri") + section.GetValue<string>("CallbackPath");
        })
    .AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
    .AddInMemoryTokenCaches();

Solution

  • Here's my nuget packages using Graph SDK v5, then we can write codes like _graphServiceClient.Me.GetAsync();

    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" NoWarn="NU1605" />
      <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" />
      <PackageReference Include="Microsoft.Identity.Web" Version="2.15.2" />
      <PackageReference Include="Microsoft.Identity.Web.UI" Version="2.15.2" />
      <PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="2.15.2" />
        <PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.15.2" />
    </ItemGroup>
    

    If we want to use Graph SDK v4, then we can use _graphServiceClient.Me.Request().GetAsync();, and the nuget packages should downgrade version, my sample using

    <PackageReference Include="Microsoft.Graph" Version="4.43.0" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.52.0" />
    <PackageReference Include="Microsoft.Identity.Web" Version="1.26.0" />
    <PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.26.0" />
    <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.26.0" />