我正在使用 Microsoft Graph 探索从我的应用访问 Azure Active Directory,直接使用 REST API(无需 SDK)。
根据文档,我可以通过 /users/{id | userPrincipalName}
使用用户的 id
或 userPrincipalName
检索用户信息。
确实可以正常工作,但对于来宾用户却不行。对于来宾用户,userPrincipalName
是类似于 name_originaldomain#EXT#@mydomain.onmicrosoft.com
的格式,尝试获取该用户会导致 404 Not Found
的错误。
这是我目前正在使用的代码:
graphClient = new HttpClient();
graphClient.BaseAddress = new Uri("https://graph.microsoft.com/v1.0/");
graphClient.DefaultRequestHeaders.Add("Authorization", $"{token.TokenType} {token.AccessToken}");
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Get,
$"users/{Uri.EscapeUriString(username)}"
);
HttpResponseMessage response = await graphClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsAsync<UserResult>();
}
// I am here with a 404
我有所不知或者做错了什么吗?