【问题标题】:List all users of AD B2C列出 AD B2C 的所有用户
【发布时间】:2023-04-03 23:15:02
【问题描述】:

我是 AD B2C 的新手,我在使用 AD Graph API 时遇到了问题。

我的目标是列出所有注册到我的 AD 的用户。我只想通过 HTTP 请求来执行此操作。

到目前为止,我发现我必须使用这个端点:

https://graph.windows.net/my_tenant.onmicrosoft.com/users?api-version=1.6

我的问题是我找不到在哪里可以获得Authorization 标头中使用的令牌。

目前,我正在通过此 URL 进行身份验证:

https://my_tenant.b2clogin.com/my_tenant.onmicrosoft.com/oauth2/v2.0/authorize?
client_id=27fb84fe-4baf-4b6b-bfe7-f2d0638f2790
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost
&response_mode=query
&scope=27fb84fe-4baf-4b6b-bfe7-f2d0638f2790%20offline_access
&state=data
&p=B2C_1_SignUporSignIn

我通过我的 redirect_uri 获得一个代码,然后我将此代码发送回此端点以获取一个令牌:

https://my_tenant.b2clogin.com/my_tenant.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1_signuporsignin

按照此处的文档:https://docs.microsoft.com/fr-fr/azure/active-directory-b2c/active-directory-b2c-access-tokens,我发送这些参数:

grant_type:authorization_code
client_secret:my_secret
client_id:27fb84fe-4baf-4b6b-bfe7-f2d0638f2790
scope:openid
code:code

问题是我没有得到像文档中显示的那样的响应。我只得到id_tokentoken_typenot_beforeid_token_expires_inprofile_info。但是,id_token 似乎对应于文档中的access_token

那么问题来了,是不是必须在 Authorization 标头中使用这个令牌才能向 AD Graph API 发送请求?

因为如果我使用此令牌,我会收到以下错误:Authentication_ExpiredToken - 您的访问令牌已过期。请在提交请求之前更新它。

提前感谢您的帮助

【问题讨论】:

标签: azure-ad-b2c


【解决方案1】:

是否必须在 Authorization 标头中使用此令牌才能向 AD Graph API 发送请求?

是的,对于来自Azure Ad GraphMicrosoft Graph API 的每个资源访问请求,您都需要传递您收到的令牌。

您的错误原因:Authentication_ExpiredToken - 您的访问令牌已过期。请在提交请求前续订:

我已经测试并成功重现了您遇到的错误。查看屏幕截图:

可能原因:

  1. 您的令牌请求端点可能有误或您已收到令牌 对于一个租户并尝试访问其他租户资源。确保您的令牌端点是 https://login.microsoftonline.com/YourB2CTenant.onmicrosoft.com/oauth2/token

  2. 检查你的资源:应该是https://graph.windows.net/

令牌请求格式:

Request Endpoint URL: 
https://login.microsoftonline.com/YourTenantName.onmicrosoft.com/oauth2/token

请求正文

grant_type:client_credentials
client_id:AppId
client_secret:AppSecret
resource:https://graph.windows.net/

请参阅下面的屏幕截图:

B2C 用户访问格式:

用户列表请求网址:

 https://graph.windows.net/YourTenantName.onmicrosoft.com/users?api-version=1.6

查看屏幕截图:

要记住的要点:

您可能会遇到Insufficient privilege error 401 在这种情况下只需分配以下权限。

许可来自: Windows Azure Active Directory

权限名称: Read all users' full profiles

权限类型: DELEGATED PERMISSIONS

查看屏幕截图:

这是您获取 azure B2C 令牌并使用该令牌访问您的用户列表的方法。

【讨论】:

    猜你喜欢
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多