azure - 访问 token 不起作用 Azure AD 自定义策略

标签 azure azure-ad-b2c

我为 Azure B2C 制定了自定义策略,添加了 jwt.ms 作为回复 URL,一切似乎都工作正常。当我登录时,我被重定向到 jwt.ms,显示 token 及其内容。然而,当我尝试将我的角度前端链接到此自定义策略时,它似乎不起作用。需要响应类型 id_token token ,但当我使用此响应类型时,我收到错误“此应用程序对此 Web 资源没有足够的权限来执行该操作。

当我使用响应类型 id_token 时,它似乎工作正常,并且我收到了带有 JWT token 的回复。

我认为问题可能出在应用程序下的隐式授予设置中,因此我尝试为所有应用程序启用访问 token ID token 应用程序但这没有帮助。

Implicit grant settings

请求如下所示:

https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/authorize
?p=b2c_1a_signup_signin&client_id={client_id}
&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2F&response_type=id_token%20token
&scope=openid%20https%3A%2F%2F{tenant}.onmicrosoft.com%2Fapi%2Fuser_impersonation
&nonce={nonce}
&state={state}

所以如果你替换字符,它看起来像这样:

https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/authorize
?p=b2c_1a_signup_signin&client_id={client_id}
&redirect_uri=http://localhost:4200/&response_type=id_token token
&scope=openid https://{tenant}.onmicrosoft.com/api/user_impersonation
&nonce={nonce}
&state={state}

我希望登录流程返回 JWT ID token 和访问 token 。

编辑: 我使用标准 Signupsignin 用户流程创建了一个全新的 B2C 租户,但我仍然遇到相同的问题。所以我得出的结论是,这不是自定义策略问题,而是Azure或前端应用程序中的配置导致的问题。

最佳答案

经过大量实验,我逐渐了解了您的问题。我猜你没有尝试正确的格式。即使我不知道你引用的是哪个文件。好吧,第一个问题是......

您收到此错误“此应用程序没有足够的权限对​​此网络资源执行该操作”

原因

您尚未在 Azure 门户上的目标应用程序中设置 API 访问。为此,您需要在应用程序中添加 API 访问

假设您的 B2C 租户应用程序上有两个已发布范围,如下所示:

enter image description here

创建 API 访问

为了避免上述错误添加 API 访问。为此,请转到您的应用程序,选择 API 访问,然后您将在顶部看到一个 添加 按钮。单击,右侧的API访问窗口将打开,只需单击确定即可。请参阅下面的屏幕截图:

enter image description here

成功API访问后,它应该如下所示

enter image description here

Note: Hope It will solve your error while you are trying to send request.

B2C id_token 和访问 token

按照以下步骤获取 Id_token

ID token 访问流程

https://YourB2CTenant.b2clogin.com/tfp/YourB2CTenant.onmicrosoft.com/YourPolicy/oauth2/v2.0/authorize?

client_id= YourApplicationId

&redirect_uri= Your reply from Application

&scope=openid

&response_type=id_token

看看下面的 Id_token:

enter image description here

Note: Just fill out the above format and paste at your browser with no space between. If is it correct then a sign in page will be prompted. After sign in you will be given id_token on your browser.

B2C 访问 token

POSTMAN中遵循以下格式:

Grant Type:authorization_code

Callback URL: Your Reply URL

AuthURL:https://YourTenant.b2clogin.com/YourTenant.onmicrosoft.com/YourFlow/PolicyName/oauth2/v2.0/authorize?

Access Token URL: https://YourTenant.b2clogin.com/YourTenant.onmicrosoft.com/oauth2/v2.0/token?p=YourFlow/PolicyName

Client ID: Your Application Id

Client Secret = Application Key

Scope= any of your Published scopes

postman 请求流程

转到 postman 并单击Authorization选项卡,然后选择typeOAuth 2.0请参阅下面的屏幕截图:

enter image description here

要获取访问 token ,请单击获取新的访问 token ,系统将提示您按照上述格式填写以下窗口。

enter image description here

enter image description here

点击请求 token 按钮后,系统将提示您登录窗口,成功登录后,您将获得access_token作为响应。如下所示:

enter image description here

Note: In B2C there is no supported way to get `Id-Token and access_token together in a single request. You have to achieve it in separate request. For more details you could refer here

如果您还有任何问题,请随时在评论中分享。谢谢您,祝您编码愉快!

关于azure - 访问 token 不起作用 Azure AD 自定义策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042801/

相关文章:

azure - 如何自行(本地)以可变比特率为 Azure 媒体服务准备视频?

azure - 从 Blob 中提取 Azure CDN 内容时,如何在其上设置 ETag?

使用 token 凭据的 Azure Synapse 身份验证

node.js - Azure Web 应用程序(Linux - Node v12 LTS)- 403 请求 header 太长

spring-boot - springboot oauth2 客户端 azure Active Directory b2c 发行者-uri 问题

Azure B2C : Generating the code_challenge

c# - 我们是否应该对 C# 中从 Microsoft.Rest.ServiceClient 派生的客户端使用单例?

Azure AD B2C - 首次登录场景中的密码重置只能通过自定义策略实现?

openid-connect - OpenID 连接中的 cookie 范围

xml - Azure B2C : Where to Create DateTime Extension Attribute to be used in Custom Policy- Portal or Custom Policy or Both?