首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Graph API无法通过项目代码工作,但相同的URL可通过AD Graph Explorer工作

问Graph API无法通过项目代码工作,但相同的URL可通过AD Graph Explorer工作
EN

Stack Overflow用户
提问于 2017-01-11 01:47:38
回答 1查看 565关注 0票数 0

我已经从Git for AAD图形API下载了一个MVC应用程序。我运行了这个应用程序,但每次都没有得到预期的结果。

为了找到错误,我使用postman运行了相同的api,并生成了下面的令牌作为响应。

代码语言:javascript
复制
   {
  "odata.error": {
    "code": "Request_ResourceNotFound",
    "message": {
      "lang": "en",
      "value": "Resource not found for the segment 'me'."
    }
  }
}

我使用下面的Get URL-

代码语言:javascript
复制
https://graph.windows.net/XXXXX/me?api-version=1.6

此外,为了验证它是否可以与AAD Grapg api explorer一起使用。登录后,一切工作正常。

下面是我调用Grapg API的代码-

代码语言:javascript
复制
 // 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浏览器上工作,而不是在本地主机上工作。

EN

回答 1

Stack Overflow用户

发布于 2017-01-11 10:03:48

为了请求Azure AD Graph REST的me端点,我们需要使用代表登录用户的委托令牌。

上面的代码是您使用客户端凭据流获取令牌的代码,它表示不包含登录用户信息的应用程序的访问令牌。

为了在MVC应用中实现这一点,我们需要在用户登录时获取授权码后获取令牌。下一次,我们可以根据登录用户从令牌缓存中获取令牌。

下面是供您参考的代码(来自here的代码示例):

代码语言:javascript
复制
result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential,
                new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41575130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档