首页 文章

Azure Active Directory图谱API - 登录用户的访问令牌

提问于
浏览
3

Azure Active Directory图Api允许您对登录用户执行操作 .

https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/signed-in-user-operations

我不知道如何代表签名用户获取Graph API的访问令牌 .

我可以为我的Web应用程序获取访问令牌,但这不代表特定用户:

var authContext = new AuthenticationContext(authorityString);
var result = await authContext.AcquireTokenAsync
             (
                "https://graph.windows.net",
                 clientCredential  // Application ID, application secret
             );
string accessToken = result.Token;

此外,当用户登录时,我会收到授权码(通过AuthorizationCodeReceived通知) . 我可以将此授权码转换为访问令牌 . 这可能是用户的访问令牌,但Graph Api无法识别 . (想想看,我不知道这个访问令牌有什么用......)

var authContext = new AuthenticationContext(authorityString);
var result = await authContext.AcquireTokenByAuthorizationCodeAsync
             (
                  authorizationCode,
                  redirectUri, // eg http://localhost:56950/
                  clientCredential // Application ID, application secret
             );
string accessToken = result.AccessToken;

使用第一个访问令牌,我可以在我的Active Directory租户上运行查询 . 例如,调用 https://graph.windows.net/thisisnotmydomain.onmicrosoft.com/users?api-version=1.6 给了我一个用户列表 .

但是如果尝试URL: https://graph.windows.net/me?api-version=1.6 ,我得到这个结果:

{
     "odata.error": {
                       "code": "Request_ResourceNotFound",
                      "message": {
                                   "lang": "en",
                                   "value": "Resource not found for the segment 'me'."
                                  }
                    }
  }

这是有道理的,因为我从未指定过用户 . 如果我使用 AcquireTokenByAuthorizactionCodeAsync 获取的访问令牌,则会收到错误消息"Access Token missing or malformed."显然,这不是图形API访问令牌 .

如何获取适用于已登录用户的Graph API访问令牌?

1 回答

相关问题