azure - oauth token 不包含有关角色或范围的信息

标签 azure oauth-2.0 oauth azure-active-directory

我已经在 AD 中创建了应用程序注册,创建了客户端 key 并公开了 API。我已经创建了应用程序角色并添加了权限(默认图形用户读取的附加权限)。我还添加了一个范围。

当我执行请求时

curl --location --request POST 'https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=<application id>' \
--data-urlencode 'client_secret=<secret>' \
--data-urlencode 'scope=api://<application id>/.default' \
--data-urlencode 'grant_type=client_credentials'

我收到包含 access_token 的响应。但是,当我解码 token 时,它不包含任何角色、范围或任何内容

{
"aud": "api://<application id>",
"iss": "https://sts.windows.net/<tenant id>/",
"iat": 1672241639,
"nbf": 1672241639,
"exp": 1672245539,
"aio": "<>",
"appid": "<application id>",
"appidacr": "1",
"idp": "https://sts.windows.net/<tenant id>/",
"oid": "<>",
"rh": "<>.",
"sub": "<>",
"tid": "<tenant id>",
"uti": "<>",
"ver": "1.0"
}

缺少什么?

/oauth2/token 返回类似的响应

{
    "aud": "<resource id>",
    "iss": "https://sts.windows.net/<tenant id>/",
    "iat": 1672241960,
    "nbf": 1672241960,
    "exp": 1672245860,
    "aio": "<>",
    "appid": "<application id>",
    "appidacr": "1",
    "idp": "https://sts.windows.net/<tenant id>/",
    "oid": "<>",
    "rh": "<>",
    "sub": "<>",
    "tenant_region_scope": "EU",
    "tid": "<tenant id>",
    "uti": "<>",
    "ver": "1.0",
    "xms_tdbr": "EU"
}

最佳答案

我尝试通过 Postman 在我的环境中重现相同的结果,并得到以下结果:

我注册了一个 Azure AD 应用程序并创建了名为 READER.ALL应用程序角色,如下所示:

enter image description here

现在我公开了一个名为 ReaderScope.ALL 的 API,如下所示:

enter image description here

现在,我通过 Postman 使用客户端凭据流生成了 token ,参数如下:

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token

client_id: <appID>
client_secret: <secret>
scope: api://<appID>/.default
grant_type:client_credentials

回应:

enter image description here

当我解码 token 时,我没有找到与以下内容相同的roles声明:

enter image description here

要在 token 中获取roles声明,您需要添加API权限,如下所示:

enter image description here

现在,选择应用程序权限并添加应用程序角色,如下所示:

enter image description here

确保向添加的权限授予管理员同意,如下所示:

enter image description here

当我现在再次生成 token 并对其进行解码时,我成功获得了 roles 声明,如下所示:

enter image description here

使用客户端凭据流程时,您将仅获得角色声明中的应用程序权限。

要在 scp 声明中获取委托(delegate)权限,您需要使用授权代码流程等交互流程。

关于azure - oauth token 不包含有关角色或范围的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74941823/

相关文章:

azure - 无法在独立的 Azure 函数中设置配置 - .NET 7

api - 如何在python中使用github api token进行请求

php - Google_Service_OAuth2 在 PHP 中是 'Undefined type'

facebook-graph-api - facebook API 中的刷新 token 和访问 token

angularjs - 在 Angular/Golang 项目中使用 JWT

c# - 继续标记 - CreateDocumentQuery ExecuteNextAsync 函数不存在

azure - 使用 Terraform 安装 AKS 时获取不受支持的 block 类型

windows-phone-7 - 获取 dropbox access_token 适用于 Windows 但不适用于 Windows Phone

azure - Terraform:创建 Azure 策略(允许的位置)- 缺少值

command-line - 安装oAuth PECL错误: Cannot install, php_dir for channel "pecl.php.net"is not writeable by current user