_graphServiceClient.Me.GetAsync() 抛出 IUserRequestBuilder 不包含 GetAsync()

问题描述 投票:0回答:1

我想根据这个示例

从GraphServiceClient获取数据

但是,这条线

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

抛出以下错误:

IUserRequestBuilder 不包含 GetAsync 的定义

我已经安装了以下 NuGet 包:

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

di是这样设置的

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();
c# asp.net asp.net-mvc microsoft-graph-api
1个回答
0
投票

这是我使用Graph SDK v5的nuget包,然后我们可以编写类似

_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>

如果我们想使用Graph SDK v4,那么我们可以使用

_graphServiceClient.Me.Request().GetAsync();
,并且nuget包应该降级版本,我的示例使用

<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" />
© www.soinside.com 2019 - 2024. All rights reserved.