确认在B2C策略中已经为身份验证响应启用了有效载荷(参见下面的代码示例)。
确认在Angular的配置中设置了正确的clientId和authorityUrl(参见下面的代码示例)。
确认在组件中正确调用了MSAL库中的acquireTokenSilent()方法,以从B2C获取身份验证令牌并解析有效载荷。
代码示例:
在B2C策略中启用有效载荷:
在Angular中的配置:
export const environment = {
production: false,
clientId: 'your-client-id-here',
authorityUrl: 'https://your-b2c-tenant.b2clogin.com/your-b2c-tenant.onmicrosoft.com/b2c_1a_signinup/',
redirectUri: 'http://localhost:4200',
scopes: ['openid', 'email', 'profile', 'offline_access', 'https://your-b2c-tenant.onmicrosoft.com/api/user_impersonation']
};
在组件中调用acquireTokenSilent()方法:
this.authService.acquireTokenSilent(['https://your-b2c-tenant.onmicrosoft.com/api/user_impersonation'])
.then((response) => {
console.log('ACQUIRED TOKEN', response);
const payload = JSON.parse(Utils.base64DecodeStringUrl(response.idToken.split('.')[1]));
console.log('PAYLOAD', payload);
})
.catch((error) => {
console.log('ERROR ACQUIRING TOKEN', error);
});