【问题标题】:Azure AD B2C authorization code flow - accessing graph.windows.netAzure AD B2C 授权代码流 - 访问 graph.windows.net
【发布时间】:2019-12-10 01:34:30
【问题描述】:

我使用 Azure AD B2C authorization code flow(下面我在 URL 和表单内容中插入换行符以获得更好的可见性)。

首先我在浏览器中打开以下网址:

https://mycompany.b2clogin.com/mycompany.onmicrosoft.com/oauth2/v2.0/authorize?
client_id=8acddeb9-e950-4d64-802c-dcc9fab4f89b&
response_type=code&
redirect_uri=https%3A%2F%2Fmy-company-site.com%2F&
response_mode=query&
scope=8acddeb9-e950-4d64-802c-dcc9fab4f89b%20offline_access%20openid&
state=arbitrary_data_you_can_receive_in_the_response&
p=B2C_susi_test

输入凭据后,我将被重定向到

https://my-company-site.com/?
state=arbitrary_data_you_can_receive_in_the_response&
code=__authorization_code__

之后我尝试请求access_token(这里是https://graph.windows.net/):

POST https://mycompany.b2clogin.com/mycompany.onmicrosoft.com/oauth2/v2.0/token?p=B2C_susi_test
User-Agent: Fiddler
Host: mycompany.b2clogin.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&
client_id=8acddeb9-e950-4d64-802c-dcc9fab4f89b
scope=openid+offline_access&
code=__authorization_code__&
redirect_uri=https%3A%2F%2Fmy-company-site.com%2F&
client_secret=__client_secret__&
resource=https%3A%2F%2Fgraph.windows.net%2F

结果我得到以下 JSON:

{
    "id_token": "__a_token__",
    "token_type": "Bearer",
    "not_before": 1564662310,
    "id_token_expires_in": 3600,
    "profile_info": "__some_info__",
    "refresh_token": "__refresh_token___",
    "refresh_token_expires_in": 1209600
}

首先这里没有access_token,但是有id_token,和docs不同。

然后我尝试使用id_token 访问https://graph.windows.net

GET https://graph.windows.net/mycompany/users/me?api-version=1.6 HTTP/1.1
User-Agent: Fiddler
Host: graph.windows.net
Authorization: Bearer __a_token__

响应 (401):

{
    "odata.error": {
        "code": "Authentication_ExpiredToken",
        "message": {
            "lang": "en",
            "value": "Your access token has expired. Please renew it before submitting the request."
        }
    }
}

任何想法为什么令牌是access_token 丢失或如何使用id_token

【问题讨论】:

    标签: oauth-2.0 azure-ad-b2c openid-connect


    【解决方案1】:

    您无法使用 Azure AD B2C 流获取 Azure AD Graph API 的访问令牌(因为此 API 不是 Azure AD B2C 注册的应用程序)。

    你必须acquire it using an Azure AD flow

    【讨论】:

      【解决方案2】:

      您无法使用 AAD B2C 应用程序注册和 B2C 用户旅程访问任何 Microsoft 第一方服务。

      如果您需要访问 Microsoft 服务,请使用 AAD 应用注册和 AAD 流。

      在 B2C 场景中,Web 服务器需要访问 Graph API,让 Web 服务器使用客户端凭据流向 B2C 目录中的 AAD 应用注册。

      【讨论】:

        猜你喜欢
        • 2017-04-20
        • 2021-08-23
        • 1970-01-01
        • 2017-01-04
        • 1970-01-01
        • 2017-08-04
        • 2023-03-25
        • 1970-01-01
        • 2021-06-20
        相关资源
        最近更新 更多