Azure AD B2C授权代码流-访问graph.windows.net

时间:2019-08-01 12:39:47

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

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

首先,我在浏览器中打开以下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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

您无法使用Azure AD B2C流获取Azure AD Graph API的访问令牌-因为此API并非Azure AD B2C注册的应用程序。

您必须acquire it using an Azure AD flow