[英]azure ad graph api not pulling user information
我正在使用azure广告图api从活动目录中提取用户个人资料数据。 我所有的输入参数都是正确的,并且令牌也通过以下代码生成。 但这不是给用户配置文件对象作为response.response.IsSuccessStatusCode始终为false。 我在这里可能是什么错误?
private readonly string graphUserUrl = "https://graph.windows.net/{0}/me?api-version=1.6"
string tenantName = "Microsoft.OnMicrosoft.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
// Config for OAuth client credentials
ClientCredential clientCred = new ClientCredential(clientId, appKey);
string resource = "https://graph.windows.net";
string token = "";
try
{
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
token = authenticationResult.AccessToken;
}
catch (AuthenticationException ex)
{
}
UserProfile profile;
string requestUrl = String.Format(CultureInfo.InvariantCulture,graphUserUrl,HttpUtility.UrlEncode(tenantId));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
//HttpResponseMessage response = await client.SendAsync(request);
HttpResponseMessage response = client.SendAsync(request).Result;
// Return the user's profile in the view.
if (response.IsSuccessStatusCode)
{
string responseString = await response.Content.ReadAsStringAsync();
profile = JsonConvert.DeserializeObject<UserProfile>
(responseString);
}
您正在使用应用程序令牌来检索用户信息。 由于令牌中没有此类登录用户信息,因此预计会出现错误。 要使用该应用程序令牌读取用户信息,我们需要更换me
使用users\\{id | userPrincipalName}
users\\{id | userPrincipalName}
,如下所示:
https://graph.windows.net/{tenant}/users/{id|userPrincipalName}?api-version=1.6
该应用程序令牌通常用在由客户端证书流获取使用的守护程序服务中。 有关此流程的更多详细信息,您可以在此处参考。
如果您想使用me
keyworld,我们需要使用委托令牌,可以使用OAuth 2代码授权流程来获取委托令牌。 并且基于预览线程 ,似乎您正在使用Web应用程序进行开发。 请在此处检查有关使用Azure AD Graph开发的代码示例以显示配置文件。 以下是获取令牌的相关代码:
string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
这是有关Azure AD身份验证安全性的有用文档:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.