【问题标题】:Azure ADB2C request token for service用于服务的 Azure ADB2C 请求令牌
【发布时间】:2021-06-29 03:34:11
【问题描述】:

上下文

我了解 Azure AD B2C 对 oauth2 client_credentials 流有限制,但我发现可以向 AD 而不是 AD B2C 再次请求令牌。

据我了解,这是对授权域的更改,所以不要使用:

https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/{policy}/oauth2/v2.0/token

我们应该使用:

https://login.microsoftonline.com/032bba1a-4a23-41d0-8713-69ad1a38cf69/oauth2/v2.0/token

问题

我的问题是,如果我向它添加一个范围,例如 https://graph.microsoft.com/openid/.default 或来自不同应用程序的其他应用程序(这是我打算使用的应用程序)https://{tenant}.onmicrosoft.com/backendtestapi/api/.default,我会收到此错误:

AADSTS500011: The resource principal named https://graph.microsoft.com/openid was not found in the tenant named xxxxx-xxxxx-xxxxx-xxxxx. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant

如果我请求此范围,我不明白为什么会出现错误。有没有大神赐教?

非常感谢!

【问题讨论】:

  • 该范围无效。关注这个:docs.microsoft.com/en-us/graph/…。对于自定义应用程序和范围,它应该采用这种格式 https://{tenant}.onmicrosoft.com/something/something/.default - 它在菜单中给出准确的值。
  • https://{tenant}.onmicrosoft.com/something/something/.defaulthttps://{tenant}.onmicrosoft.com/backendtestapi/api/.default 之间有什么区别?无法理解您的建议。

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


【解决方案1】:

请注意,Microsoft Graph 是官方 API,其 scope 对于 V2.0 具有固定的写入格式 https://graph.microsoft.com/.default

https://graph.microsoft.com/openid/.default 更改为https://graph.microsoft.com/.default 可以解决此问题,您将获得用于调用Microsoft Graph API 的访问令牌。

如果您有自己的需要客户端凭据流的自定义 Web API,您应该按照Exposing application permissions (app roles) 在您的 API 应用注册中公开该 API。然后在您的客户端应用注册中通过Add permissions to access your web API配置应用权限。

之后,您可以获得scope 格式。通常是api://{app id of the API app registration}/.default。如果自定义API应用注册的Application ID URI为https://{tenant}.onmicrosoft.com/backendtestapi/api/,那么scope应该是https://{tenant}.onmicrosoft.com/backendtestapi/api/.default

但是有一件重要的事情你需要了解。这是因为客户端凭据流在没有用户的情况下发出应用程序令牌,在这种情况下它与 B2C 身份验证无关。换句话说,您只需“借用” B2C 租户来创建应用程序注册。 B2C不用于身份认证。

顺便说一句,应用程序权限通常需要管理员同意。不要忘记在客户端应用注册或use the adminconsent endpoint 上点击“为{您的租户} 授予管理员同意”。

【讨论】:

  • 感谢您的回答。我理解您所说的,但是如果我在再次验证 login.microsoftonline.com 权限时尝试请求 API 权限,我会收到该权限在该租户中不可用的错误。那有意义吗?谢谢
  • @JohnySerpa 您是否按照docs.microsoft.com/en-us/azure/active-directory/develop/… 创建了 API 应用注册?如果您使用的是多租户应用程序,您应该使用管理员同意端点:https://login.microsoftonline.com/{tenant-id of other tenant}/adminconsent?client_id={client-id} 将应用程序添加到其他租户。
  • API 是否应该是 B2C 应用程序?我的意思是,在通过 AD B2C 创建应用程序时,我是否应该选择最后一个选项“任何身份提供者或组织目录中的帐户(用于通过用户流对用户进行身份验证)”?我对那个感到困惑。我的前端应用程序是 B2C,它必须是对用户进行身份验证。但是我的后端应用程序,我不确定它是否也必须是 B2C 应用程序。谢谢
  • @JohnySerpa 没有。不需要选择 B2C 应用。
  • 我想我需要它。否则我会收到authentication failed due to: In Strategy.prototype.jwtVerify: We did not receive a token we know how to validate 的错误。我认为我无法在非 B2C 应用程序中验证 B2C 令牌。如果我错了,你能告诉我吗?
猜你喜欢
  • 2016-07-17
  • 1970-01-01
  • 2012-06-22
  • 2022-08-22
  • 2014-01-20
  • 2020-07-05
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多