Azure AD B2C 授权代码流 - 访问 graph.windows.net
Azure AD B2C authorization code flow - accessing graph.windows.net
我使用 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
的任何想法?
您无法使用 AAD B2C 应用程序注册和 B2C 用户旅程访问任何 Microsoft 第一方服务。
如果您需要访问 Microsoft 服务,请使用 AAD 应用程序注册和 AAD 流。
在 B2C 场景中,Web 服务器需要访问 Graph API,让 Web 服务器使用客户端凭据流针对 B2C 目录中的 AAD 应用程序注册。
您无法获取 Azure AD Graph API 的访问令牌 -- 因为此 API 不是 Azure AD B2C 注册的应用程序 -- 使用 Azure AD B2C 流.
我使用 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
的任何想法?
您无法使用 AAD B2C 应用程序注册和 B2C 用户旅程访问任何 Microsoft 第一方服务。
如果您需要访问 Microsoft 服务,请使用 AAD 应用程序注册和 AAD 流。
在 B2C 场景中,Web 服务器需要访问 Graph API,让 Web 服务器使用客户端凭据流针对 B2C 目录中的 AAD 应用程序注册。
您无法获取 Azure AD Graph API 的访问令牌 -- 因为此 API 不是 Azure AD B2C 注册的应用程序 -- 使用 Azure AD B2C 流.