gpt4 book ai didi

node.js - 具有服务主体和 key 的 Node.js REST 端点的 Azure AD 身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:54 25 4
gpt4 key购买 nike

我正在尝试调用受 AzureAD 身份验证保护的 REST API。从 Postman REST 客户端来看,它的工作原理如下:https://example.com/getexample header : 授权: Bearer 。如果我以交互方式获取用户的 token (例如 device_code 或 MFA),这非常有用。

由于我想以非交互方式运行代码,因此我尝试通过服务主体对 REST 端点进行身份验证。

REST 服务器是在带有 azure-passport node.js 包的 Nodejs 中构建的。

我已经创建了服务主体( native 应用程序)和 secret 。我能够从这个包以及下面的curl命令获取access_token

curl -X POST -d 'grant_type=client_credentials&client_id=[client id]&client_secret=[client secret]&resource=[client id of the server]' https://login.microsoftonline.com/[tenant]/oauth2/token

但是如果我将生成的 token 传递给 REST 端点,我会得到 401。

请帮助如何使用服务主体和 key 对自定义 REST 端点进行身份验证。

以下是我为服务主体(服务器和客户端)所做的配置详细信息

服务器 SP(node.js 应用程序)创建服务主体,添加User.Read API权限。管理员授予API权限。在“公开 API”下创建了一个自定义范围(API.Access),并选择了“管理员和用户”可以授予。在 Node.js 应用程序中,我仅使用 user.read 范围

客户端SP( postman )创建服务主体,在 API 权限下添加服务器 SP(客户范围)使用curl命令在不传递任何范围的情况下获取访问 token 。

最佳答案

要以非交互方式运行代码,您用来获取访问 token 的方式是需要应用程序权限的客户端凭据流。 Here是委托(delegate)权限和应用程序权限之间的区别。

您可以define the application permission通过编辑服务器应用程序的 list 。

enter image description here

"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"description": "Apps that have this role have the ability to invoke my API",
"displayName": "Can invoke my API",
"id": "fc803414-3c61-4ebc-a5e5-cd1675c14bbb",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "myTestRole"
}
]

然后您的客户端应用程序可以添加应用程序权限(从您的服务器应用程序)。

enter image description here

关于node.js - 具有服务主体和 key 的 Node.js REST 端点的 Azure AD 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57209297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com