我试图在我的react应用程序中使用Microsoft B2C AD,并使用图形SDK尝试在react应用程序中获取用户信息。
我能够成功地登录,使用用户流,并获得ID-令牌登录后。
现在,当我尝试使用Graph获取用户配置文件或使用acquireTokenSilent方法获取令牌时,我得到的是一个不同的错误:-
import React from 'react';
import {
AuthenticatedTemplate,
UnauthenticatedTemplate,
useMsal,
} from '@azure/msal-react';
import { loginRequest } from '../authConfig';
import { AuthCodeMSALBrowserAuthenticationProvider } from '@microsoft/microsoft-graph-client/authProviders/authCodeMsalBrowser';
import { InteractionType } from '@azure/msal-browser';
import { Client } from '@microsoft/microsoft-graph-client';
const Home = () => {
const { instance } = useMsal();
const { accounts } = useMsal();
let graphClient = undefined;
const scopes = [
'openid',
'offline_access',
'https://my-tanent.onmicrosoft.com/my-app/user.read',
];
const authProvider = new AuthCodeMSALBrowserAuthenticationProvider(instance, {
account: accounts[0],
scopes,
interactionType: InteractionType.Popup,
});
function ensureClient() {
graphClient = Client.initWithMiddleware({
authProvider: authProvider,
});
return graphClient;
}
async function getUser() {
ensureClient();
// Return the /me API endpoint result as a User object
const user = await graphClient
.api('/me')
// Only retrieve the specific fields needed
.select('displayName,mail,mailboxSettings,userPrincipalName')
.get();
console.log(user);
}
//just to check the token
const getAccessToken = async () => {
try {
const token = await instance.acquireTokenSilent({
scopes,
});
console.log(token);
} catch (error) {
console.error(error);
}
};
return (
<>
<UnauthenticatedTemplate>
<h3 className="h3">Login</h3>
<button onClick={() => instance.loginPopup(loginRequest)}>Login</button>
</UnauthenticatedTemplate>
<AuthenticatedTemplate>
<div className="App">
<header className="App-header">
<p>Hello {accounts[0]?.name}!</p>
<button
onClick={() =>
instance.logoutRedirect({ postLogoutRedirectUri: '/' })
}
>
Logout
</button>
<button onClick={() => getUser()}>Get User</button>
<button onClick={() => getAccessToken()}>AcquireTokenSilent</button>
</header>
</div>
</AuthenticatedTemplate>
</>
);
};
图SDK = Error:访问令牌验证失败。无效观众。在第二个方法中,我得到了一个访问令牌,但是当我解密它时,它看起来像是ID-令牌,因为aud是客户端ID:- 请查看此以了解更多有关令牌的信息。。
"exp": 1660120614,
"nbf": 1660117014,
"aud": "my-client-id",
"sub": "df5a1676-74bb-46b6-9116-a5fa125941da",
"name": "Parteek",
"postalCode": "246401",
"tfp": "B2C_1_susi",
"nonce": "9f13aefb-1a6e-4818-a39e-4f1369ca67d8",
"scp": "user.read",
"azp": "93dee6ed-1f87-4f0f-bedb-fd3f1a03b0ed",
"ver": "1.0",
"iat": 1660117014
第二次尝试与作用域更改
const scopes = [
'openid',
'offline_access',
'https://graph.microsoft.com/.default',
];
具有此范围值:-
Graph错误=访问令牌未定义:从PublicClientApplication acquireTokenSilent接收到的空访问令牌:访问令牌为空。
对我的蔚蓝账户的权限
我不知道为什么我会收到这些错误,这是与权限有关还是我以错误的方式实现了它?
发布于 2022-08-16 12:15:46
另外,请注意:如果您使用受支持的帐户类型作为任何身份提供者在B2C租户中注册应用程序,那么您可能无法使用该应用程序执行图形操作。
参考资料:
https://stackoverflow.com/questions/73303046
复制相似问题