【问题标题】:Azure OAuth2 Code Flow - no groups claim in access token (and no "hasgroups" either)Azure OAuth2 代码流 - 访问令牌中没有组声明(也没有“hasgroups”)
【发布时间】:2020-01-09 16:19:01
【问题描述】:

我正在使用 OAuth2 代码流并尝试将用户组重新放入我的访问令牌中。我读到的所有内容都说我应该看到一个 group 声明或一个 hasgroups 声明,但我都没有看到。

我已更改客户端应用的应用注册清单中的以下字段:

"groupMembershipClaims": "SecurityGroup",
"optionalClaims": {
        "idToken": [],
        "accessToken": [
            {
                "name": "groups",
                "source": null,
                "essential": false,
                "additionalProperties": [
                    "dns_domain_and_sam_account_name"
                ]
            }
        ],
        "saml2Token": []
    },

这是我的登录重定向 url (login.microsoftonline.com) 的查询字符串示例...

client_id=<clientId>
&response_type=code
&redirect_uri=<redirectUri>
&response_mode=query
&scope=<appScope>%20offline_access
&state=67890

这是我使用 authCode (login.microsoftonline.com/{tenantId}/oauth2/v2.0/token) 请求令牌的查询字符串示例

client_id=<clientId>
&scope=<uriEncodedScopes>%20offline_access
&redirect_uri=<uriEncodedRedirectUri>
&code=<authCode>
&client_secret=<uriEncodedClientSecret>
&grant_type=authorization_code

一切都很好,但我不知道如何在我的令牌中获取组信息。

更新

我在两个 url 的作用域中都添加了 %20openid,现在我得到了一个 id_token,但我仍然没有在任何一个令牌中看到“groups”或“hasgroups”。

更新

我刚刚将相同的清单更改(groupMembership、optionalClaims)添加到我的 API 应用注册(而不是我的客户端)——公开范围的 API,我看不到任何更改。访问令牌和 Id 令牌根本没有对组的任何引用。

【问题讨论】:

    标签: azure oauth-2.0 claims


    【解决方案1】:

    根据我的测试,它应该可以工作。而您只需要在您的API App Registration 中配置groupMembershipClaimsoptionalClaims,参考下面的reason

    你可以参考我下面的测试样例,我用工作账户或个人账户都试过了。

    请求授权码(api://3e013fde-xxxxxxa422f3/User.Test是我的API权限):

    https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize?
    client_id=xxxxxxxxxxxxxxx
    &response_type=code
    &redirect_uri=https://localhost
    &response_mode=query
    &scope=openid offline_access api://3e013fde-xxxxxxa422f3/User.Test
    &state=12345
    

    请求令牌:

    解码https://jwt.io/ 中的令牌,包括groups 声明。


    注意:

    我的客户端App和API App都是今天创建的,我想old App Registration(it is not existing in the portal currently)app registration portal(it has been moved to the new App Registrations)new App Registration(theApp Registrationin portal currently)中创建的应用程序可能会有一些差异。

    从这个doc:

    在我的测试中还有一个奇怪的事情,当我创建一个新的API App Registration时,只设置"groupMembershipClaims": "SecurityGroup"而不设置optionalClaims,manifest会如下所示。

    "groupMembershipClaims": "SecurityGroup",
    "optionalClaims": {
            "idToken": [],
            "accessToken": [],
            "saml2Token": []
        }
    

    那么Access token 将不包括groupsID tokengroups

    如果我用你的设置,Access token 将拥有groups

    "groupMembershipClaims": "SecurityGroup",
    "optionalClaims": {
            "idToken": [],
            "accessToken": [
                {
                    "name": "groups",
                    "source": null,
                    "essential": false,
                    "additionalProperties": [
                        "dns_domain_and_sam_account_name"
                    ]
                }
            ],
            "saml2Token": []
        }
    

    但是当我设置它返回到

    "groupMembershipClaims": "SecurityGroup",
    "optionalClaims": {
            "idToken": [],
            "accessToken": [],
            "saml2Token": []
        }
    

    Access token 仍然有 groups

    来自门户 - Token configuration (preview) 和这个文档 - Configure group claims for applications with Azure Active Directory (Public Preview),该功能应该是预览版,它可能是一个错误(我不确定)。

    因此,总之,我建议您使用两个新应用来尝试一下。

    【讨论】:

    • 感谢您的回答。我目前无法尝试,因为我需要我的管理员增加我们在此测试帐户上的支出限额,但与此同时 - 您的 Azure AD 级别是多少?我使用的是默认(免费)版本。我需要升级到高级版 AD 吗?我很好奇是否需要在我的客户端应用注册中配置“用户和组”以将我的组添加到那里。
    • @TimHardy 我也在使用 AAD 免费版。而且我认为无需在 SP(企业应用程序)-> Users and Groups 中添加您的组,只需确保属性-> 需要用户分配-> No测试我的解决方案的最佳方法是创建两个新应用,因为有一个我无法重现的错误。
    • 我终于能够让我的 Azure 工作区重新上线。重新创建 api 应用程序注册后,我开始在我的令牌中看到这些组。感谢您的帮助。
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2020-01-16
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多