Authorization_IdentityNotFound Microsoft Graph API 请求

Authorization_IdentityNotFound on Microsoft Graph API request

我正在尝试在我的企业中开发一个应用程序,并且我已经按照 this tutorial 访问 AD 用户信息。含义:

  1. 我在 https://apps.dev.microsoft.com/
  2. 中创建了一个应用程序
  3. 我在应用程序权限中设置了User.Read.All,在委派权限
  4. 中设置了User.Read

完成此操作后,我能够成功登录(Azure AD OAuth2,https://graph.microsoft.com/ 作为资源,User.Read 作为范围)并从 https://graph.microsoft.com/v1.0/me 获得正确的响应。

  1. 向管理员询问委派权限

有了这个,我的管理员可以在 azure portal 中看到我的应用程序拥有他自己同意的两个权限。

这是有效的,因为我要求同事登录并且我可以从 https://graph.microsoft.com/v1.0/me 得到正确的回复,即使他甚至没有被提示同意这个(在管理员同意用户的权限之前)提示)

  1. https://login.microsoftonline.com/common/oauth2/token 请求令牌 client_credentials 作为 response_type

  2. 领取令牌!

  3. https://graph.microsoft.com/v1.0/users 发出 GET 请求并接收:

    {
        "error": {
            "code": "Authorization_IdentityNotFound",
            "message": "The identity of the calling application could not be established.",
            "innerError": {
                "request-id": "b2d9ec62-0b65-44eb-9e0f-4aec52b45750",
                "date": "2017-03-22T19:19:48"
            }
        }
    }
    

此外,向 https://graph.microsoft.com/v1.0/me returns 发出请求:

{
  "error": {
    "code": "BadRequest",
    "message": "Current authenticated context is not valid for this request",
    "innerError": {
      "request-id": "047e2ba9-a858-45fc-a0dd-124e1db503f3",
      "date": "2017-03-22T19:39:25"
    }
  }
}

这让我相信微软知道这个令牌并且知道它没有冒充任何用户。

我一直在寻找有关 Azure AD 和 Microsoft Graph 身份验证的文档,但我只找到了博客文章,而且所有文章似乎都已过时(尽管大多数功能都处于预览状态)。 如果你能指出我正确的方向,我会感谢你。

我也在 SO 上找到了 this and this 类似的问题,但它们都没有得到解答。

更新,在

之后

谢谢你,丹, 我已经使用了我的组织域名,而且我还可以获得一个令牌。

现在 https://graph.microsoft.com/v1.0/users/ 的响应是:

{
  "error": {
    "code": "Authorization_RequestDenied",
    "message": "Insufficient privileges to complete the operation.",
    "innerError": {
      "request-id": "3f190b47-73f5-4b29-96f9-54ed3dbc3137",
      "date": "2017-03-23T11:07:15"
    }
  }
}

这没有任何意义,因为在 Azure 门户中我有 User.Read.All 作为应用程序权限(已经得到管理员同意)。

我认为问题出在令牌请求上,无论我发送 scope,returns 都成功,即使我编造了一个。

例如:

POST https://login.microsoftonline.com/<domain>/oauth2/token

client_id:*******
client_secret:*******
resource:https://graph.microsoft.com/
grant_type:client_credentials
scope:Foo.Bar

Returns:

{
  "token_type": "Bearer",
  "expires_in": "3599",
  "ext_expires_in": "0",
  "expires_on": "1490271617",
  "not_before": "1490267717",
  "resource": "https://graph.microsoft.com/",
  "access_token": *****
}

/me 段是当前登录用户的快捷方式或别名。对 /me 的请求永远不会与应用程序令牌一起使用,因为它不包含任何用户上下文(或登录用户)——因此不包含错误。不过,我们也许可以改善此错误 ;)

相信在使用客户端凭证流程时,您需要指定您想要令牌的实际租户。

如果您的应用程序在工作或学校(组织)上下文中执行此操作,那么对于 https://login.microsoftonline.com/common/oauth2/tokencommon 替换为 tenantId 或域名,并查看是否可行。

如果您正在关注 https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oauth-client-creds,看起来我们可能有一些需要修复的文档错误...

希望这对您有所帮助,

我有两个问题,都没有涉及文档:

  • 对于客户端凭据,如果该应用属于工作或学校(组织)上下文,则对于https://login.microsoftonline.com/common/oauth2/token替换common使用 tenantId 或域名(感谢 Dan Kershaw

  • 对于 https://graph.microsoft.com/v1.0/usershttps://graph.microsoft.com/v1.0/users/{id | userPrincipalName} 您需要 Directory.Read.All 许可。

:

    当您在 OAuth 工作流中请求 User.Read 时,
  • User.Read.All 与 Microsoft 停止向用户请求权限(委派)有关。在 Release Notes.
  • 中检查此问题和其他与权限相关的问题
  • 我已将此 issue 添加到 Microsoft Graph 文档中!