我有一个客户端应用程序,可以向web API发出请求。我需要从web api向图形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);
我不知道我在这里漏掉了什么。会很感谢你的帮助。
https://stackoverflow.com/questions/51963788
复制相似问题