3

我正在将应用程序从 .NET Core 2.2 升级到 .NET 5.0。在此过程中,我需要更新 Microsoft.Graph 和 Microsoft.Identity.Web。我更新为使用新配置,但是当我尝试获取用户详细信息时,Graph 抛出 System.NullReferenceException。

这是我的服务配置

services.AddMicrosoftIdentityWebApiAuthentication(configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph(configuration.GetSection("MicrosoftGraph"))
    .AddInMemoryTokenCaches();

我的 appsettings.json 确实具有所需的值

"AzureAd": {
    "ClientId": "xxx.xxx......",
    "Domain": "domain.com",
    "Instance": "https://login.microsoftonline.com/",
    "TenantId": "xxxxxx......",
    "ClientSecret": "xxxxxxxxx...."
},
"MicrosoftGraph": {
    "BaseUrl": "https://graph.microsoft.com/v1.0",
    "Scopes": "user.read user.read.all"
}

这是调用图形 API 的代码

var user = await _graphServiceClient.Users["username"].Request().GetAsync();

该代码适用于我使用旧界面配置 Graph 并使用旧版本 Microsoft.Identity.Web 获取静默令牌的当前应用程序。不知道为什么新版本似乎无法获得 Graph 令牌。应用程序的身份验证通常有效,并且我确实有一个经过身份验证的用户,它仅在调用 MS Graph 时失败。

这是错误的参考日志条目

Method: Handle. Line: 73. Code: generalException
Message: An error occurred sending the request.

Status Code: 0
Microsoft.Graph.ServiceException: Code: generalException
Message: An error occurred sending the request.

---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Identity.Web.MergedOptions.PrepareAuthorityInstanceForMsal()
at Microsoft.Identity.Web.TokenAcquisition.BuildConfidentialClientApplication(MergedOptions mergedOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetOrBuildConfidentialClientApplication(MergedOptions mergedOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForUserAsync(IEnumerable1 scopes, String authenticationScheme, String tenantId, String userFlow, ClaimsPrincipal user, TokenAcquisitionOptions tokenAcquisitionOptions) at Microsoft.Identity.Web.TokenAcquisitionAuthenticationProvider.AuthenticateRequestAsync(HttpRequestMessage request) at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.UserRequest.GetAsync(CancellationToken cancellationToken)

谢谢

4

0 回答 0