来自Web API的Access Graph API会引发未经授权的错误v2.0

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

我有一个客户端应用程序,它向Web API发出请求。我需要从web api向graph api发出请求以获取登录用户的upn声明。我成功获得了访问令牌,但是当我尝试请求图API时,它会抛出一个未授权的错误。这是代码:

ConfidentialClientApplication cl = new ConfidentialClientApplication(
    clientId,
    "https://login.microsoftonline.com/common/v2.0/token",
    "https://localhost:44321/",
    new ClientCredential(appKey),
    new TokenCache(), null);

AuthenticationResult authResult = await cl.AcquireTokenForClientAsync(new string[] { "https://graph.microsoft.com/.default" });

string oid = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

client = new HttpClient();
request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/users/" + oid);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);

response = await client.SendAsync(request);

我无法弄清楚我在这里缺少什么。非常感谢你的帮助。

asp.net azure-active-directory azure-ad-graph-api msal
1个回答
0
投票

您可以尝试使用特定于租户的端点来获取访问令牌,而不是使用“客户端凭据”流的“公共”端点。

https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token

此外,您还可以检查已收到的访问令牌的应用程序权限范围。您可以使用Qazxswpoi等JWT令牌解码器工具来验证令牌上的声明

© www.soinside.com 2019 - 2024. All rights reserved.