我正在开发一个Angular 10应用程序,利用Azure B2C进行策略和用户管理。我将我的应用程序注册在Azure Active Directory中,作为单页面应用程序,没有勾选隐式选项。我使用msal.js 2.0 @azure/msal-browser
来登录B2C并使用代码流检索id和访问令牌。我设置了我的配置,创建了msal对象,定义了重定向承诺,然后稍后使用适当的用户范围调用loginRedirect。页面正确重定向。
然而,在我登录后,tokenResponse返回null。我尝试更改权限和范围,但它始终返回null。如何使handleRedirectPromise
返回有效的令牌响应呢?
以下是我的代码:
private msalConfig: Msal.Configuration = {
auth: {
clientId: xxxx-xx-xx-xx-xxxxx,
authority: 'https://login.microsoftonline.com/common',
redirectUri: 'https://localhost:4200'
},
cache: {
cacheLocation: 'sessionStorage',
storeAuthStateInCookie: false
},
};
private loginRequest: Msal.RedirectRequest = {
scopes: ['user.read'],
};
const msalInstance = new Msal.PublicClientApplication(this.msalConfig);
msalInstance
.handleRedirectPromise()
.then((tokenResponse: Msal.AuthenticationResult) => {
let accountObj = null;
if (tokenResponse !== null) {
accountObj = tokenResponse.account;
const id_token = tokenResponse.idToken;
const access_token = tokenResponse.accessToken;
console.log('id_token', id_token);
console.log('access_token', access_token);
}
})
.catch(error => {
authStore.loginError$.next(true);
console.error(error);
});
msalInstance.loginRedirect(this.loginRequest);
编辑:
我也尝试过在msalConfig
对象中使用authority: `https://<tenant-name>.b2clogin.com/<tenant-name>.onmicrosoft.com/<policy-name>
和https://login.microsoftonline.com/tfp/{tenant}.onmicrosoft.com/B2C_1_SiupIn
作为授权,以及在loginRequest
中使用scopes: ['openid']
。当我使用这个时,在尝试登录时会在浏览器中得到以下错误:
zone-evergreen.js:1068 GET https://login.microsoftonline.com/common/discovery/instance?api-version=1.1&authorization_endpoint=https://<tenant>.b2clogin.com/<tenant>.onmicrosoft.com/b2c_1_defaultsigninsignup/oauth2/v2.0/authorize 400 (Bad Request)
core.js:4197 ERROR Error: Uncaught (in promise): ClientAuthError: endpoints_resolution_error: Error: could not resolve endpoints. Please check network and try again. Detail: ClientConfigurationError: untrusted_authority: The provided authority is not a trusted authority. Please include this authority in the knownAuthorities config parameter.
ClientAuthError: endpoints_resolution_error: Error: could not resolve endpoints. Please check network and try again. Detail: ClientConfigurationError: untrusted_authority: The provided authority is not a trusted authority. Please include this authority in the knownAuthorities config parameter.