我已经从Git for AAD图形API下载了一个MVC应用程序。我运行了这个应用程序,但每次都没有得到预期的结果。
为了找到错误,我使用postman运行了相同的api,并生成了下面的令牌作为响应。
{
"odata.error": {
"code": "Request_ResourceNotFound",
"message": {
"lang": "en",
"value": "Resource not found for the segment 'me'."
}
}
}我使用下面的Get URL-
https://graph.windows.net/XXXXX/me?api-version=1.6此外,为了验证它是否可以与AAD Grapg api explorer一起使用。登录后,一切工作正常。
下面是我调用Grapg API的代码-
// Get the access token from the cache
string userObjectID =
ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")
.Value;
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
//AuthenticationContext authContext = new AuthenticationContext(authority,
// new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, appKey);
AuthenticationContext authContext = new AuthenticationContext(authority, true);
result = await authContext.AcquireTokenAsync(graphResourceId.ToString(), credential);
var Token = result.AccessToken;
//// AcquireTokenSilentAsync
//result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential,
// new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
// Call the Graph API manually and retrieve the user's 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", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);
// Return the user's profile in the view.
if (response.IsSuccessStatusCode)
{
string responseString = await response.Content.ReadAsStringAsync();
profile = JsonConvert.DeserializeObject<UserProfile>(responseString);
}你们能告诉我我的代码有什么问题吗?为什么它在AAD浏览器上工作,而不是在本地主机上工作。
发布于 2017-01-11 10:03:48
为了请求Azure AD Graph REST的me端点,我们需要使用代表登录用户的委托令牌。
上面的代码是您使用客户端凭据流获取令牌的代码,它表示不包含登录用户信息的应用程序的访问令牌。
为了在MVC应用中实现这一点,我们需要在用户登录时获取授权码后获取令牌。下一次,我们可以根据登录用户从令牌缓存中获取令牌。
下面是供您参考的代码(来自here的代码示例):
result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential,
new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));https://stackoverflow.com/questions/41575130
复制相似问题