Azure SSO 无法通过访问 token 获取用户信息

标签 azure single-sign-on

我正在尝试在我的 SPA 应用程序中实现 Azure SSO。因此,我能够对用户进行身份验证,并作为响应,我取回访问 token 。

enter image description here

之后,我尝试通过此网址获取用户基本配置文件:

https://graph.microsoft.com/v1.0/me

授权为不记名 token 。但由于某种原因,我收到以下错误响应

{
    "error": {
        "code": "UnknownError",
        "message": "",
        "innerError": {
            "date": "2023-08-31T09:55:43",
            "request-id": "eaf36ab5-aba3b1",
            "client-request-id": "eaf36ab5-aba3-49b1"
        }
    }
}

我不知道出了什么问题。 azure上的应用程序设置有问题吗? 请检查我当前的设置:

enter image description here enter image description here

谢谢。

最佳答案

The error occurred as your token don't have User.Read permission in scp claim that is mandatory for reading user info.

我注册了一个 Azure AD 应用程序并添加了 User.Read 权限,如下所示:

enter image description here

现在,我通过 Postman 使用授权代码流生成 token ,参数如下:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id: <app_ID>
grant_type:authorization_code
scope: openid profile
code: code
redirect_uri: https://jwt.ms
client_secret: <secret> 

回应:

enter image description here

当我使用此 token 获取用户信息时,我也遇到了相同的错误,如下所示:

GET https://graph.microsoft.com/v1.0/me

回应:

enter image description here

To resolve the error, make sure to generate access token with User.Read scope.

当我在浏览器中使用 User.Read 范围运行授权请求时,出现了以下同意屏幕:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize? 
client_id=appId
&redirect_uri=https://jwt.ms
&response_type=code  
&response_mode=query  
&scope= openid profile User.Read
&state=12345

enter image description here

同意上述提示后,我在地址栏成功获取了授权码:

enter image description here

现在,我使用此代码生成具有 User.Read 范围的访问 token ,并获得如下 token :

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id: <app_ID>
grant_type:authorization_code
scope: openid profile
code: code
redirect_uri: https://jwt.ms
client_secret: <secret> 

回应:

enter image description here

当我使用此 token 获取用户信息时,我成功获得了响应,如下所示:

GET https://graph.microsoft.com/v1.0/me

回应:

enter image description here

就您而言,请确保在 scope 参数中传递 User.Read

关于Azure SSO 无法通过访问 token 获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77014771/

相关文章:

c# - 如何实现基于密码流程的IdentityServer4 SSO?

安卓单点登录

azure - 在 Azure 批处理上执行任务

javascript - 如何使用javascript连接azure服务总线主题

azure - 使用 CSI 驱动程序将 AKV 与 AKS 集成后从/mnt/secrets-store/读取值

azure - 如何连接到没有 PowerShell 订阅的 Azure AD 租户

login - 如何将来自 Google、Facebook、Yahoo 等的社交登录服务添加到我的网站?

c# - 登录失败,因为 AudienceUris 中不存在指定的受众

c# - 跨多个 SP 的 SAML 身份验证

azure - Azure 应用服务部署槽的用例 - 应用版本的变化