Azure MSAL - 身份验证无法实现用户信息请求
Azure MSAL - Authentication cannot realize userinfo request
我在我的 angular 应用程序中使用 隐式流 实现了 msal 库。我可以使用 Microsoft 广告帐户登录,并且我拥有 api 使用身份验证请求所需的令牌。
当我使用 https://jwt.ms/ 解码令牌时,我可以看到 aud 密钥具有我的 客户端应用程序 ID 的值,用户信息的 url 应该是什么样子?
如果我将使用文档中描述的这些端点,我会遇到无效令牌错误:
https://graph.microsoft.com/oidc/userinfo 或
https://graph.microsoft.com/v1.0/me/
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token validation failure. Invalid audience.",
"innerError": {
"request-id": "8177b64f-c899-48c3-aceb-xxxxxxxxx",
"date": "2020-03-18T07:41:04"
}
}
}
我忘记了我的模块配置。
...
MsalModule.forRoot({
auth: {
clientId: environment.CLIENT_ID,
redirectUri: environment.REDIRECT_URI,
postLogoutRedirectUri: environment.POST_LOGOUT_REDIRECT_URI,
authority: environment.AUTHORITY,
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: true,
}
},
{
popUp: false,
consentScopes: [
'user.read',
'openid',
'profile',
],
unprotectedResources: [],
protectedResourceMap: [
['https://graph.microsoft.com/v1.0/me', ['user.read']]
]
})
...
其中:
权限 url: 权限: 'https://login.microsoftonline.com/xxxxtenant_idxxxx'
client id: 应用注册客户端id
如果您想调用 Microsoft Graph API,您需要获取具有正确(Microsoft graph api)受众的访问令牌。
当您获取访问令牌时,您应该使用 https://graph.microsoft.com/User.Read
或 User.Read
作为范围。您可以检查 app.module.ts
中的值
此外,请记住在 Azure 门户上为您的应用程序添加 User.Read
权限。应用注册->你的应用->API权限->添加权限->选择Microsoft GraphAPI->委派权限->勾选你需要的权限
更新:
参考:
MSAL Angular to login, logout, protect a route, and acquire an access token for Microsoft Graph.
已解决。问题出在指定端点。当我从 angular 请求图形时,应用程序拦截器会处理此端点并使用图形范围 (user.read),然后将分配正确的令牌。
在我尝试在授权后复制令牌并将其粘贴到邮递员之前。所以是的,令牌根本无效。谢谢你的帮助。
我在我的 angular 应用程序中使用 隐式流 实现了 msal 库。我可以使用 Microsoft 广告帐户登录,并且我拥有 api 使用身份验证请求所需的令牌。 当我使用 https://jwt.ms/ 解码令牌时,我可以看到 aud 密钥具有我的 客户端应用程序 ID 的值,用户信息的 url 应该是什么样子? 如果我将使用文档中描述的这些端点,我会遇到无效令牌错误: https://graph.microsoft.com/oidc/userinfo 或 https://graph.microsoft.com/v1.0/me/
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token validation failure. Invalid audience.",
"innerError": {
"request-id": "8177b64f-c899-48c3-aceb-xxxxxxxxx",
"date": "2020-03-18T07:41:04"
}
}
}
我忘记了我的模块配置。
...
MsalModule.forRoot({
auth: {
clientId: environment.CLIENT_ID,
redirectUri: environment.REDIRECT_URI,
postLogoutRedirectUri: environment.POST_LOGOUT_REDIRECT_URI,
authority: environment.AUTHORITY,
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: true,
}
},
{
popUp: false,
consentScopes: [
'user.read',
'openid',
'profile',
],
unprotectedResources: [],
protectedResourceMap: [
['https://graph.microsoft.com/v1.0/me', ['user.read']]
]
})
...
其中: 权限 url: 权限: 'https://login.microsoftonline.com/xxxxtenant_idxxxx' client id: 应用注册客户端id
如果您想调用 Microsoft Graph API,您需要获取具有正确(Microsoft graph api)受众的访问令牌。
当您获取访问令牌时,您应该使用 https://graph.microsoft.com/User.Read
或 User.Read
作为范围。您可以检查 app.module.ts
此外,请记住在 Azure 门户上为您的应用程序添加 User.Read
权限。应用注册->你的应用->API权限->添加权限->选择Microsoft GraphAPI->委派权限->勾选你需要的权限
更新:
参考:
MSAL Angular to login, logout, protect a route, and acquire an access token for Microsoft Graph.
已解决。问题出在指定端点。当我从 angular 请求图形时,应用程序拦截器会处理此端点并使用图形范围 (user.read),然后将分配正确的令牌。 在我尝试在授权后复制令牌并将其粘贴到邮递员之前。所以是的,令牌根本无效。谢谢你的帮助。