afr*_*111 6 azure-ad-b2c azure-ad-msal msal.js
我正在开发一个 Angular 10 应用程序,它利用 Azure B2C 进行策略和用户管理。我在 Azure Active Directory 中将我的应用注册设置为单页应用,但未选中隐式选项。我正在使用 msal.js 2.0@azure/msal-browser登录 B2C 并使用代码流检索 ID 和访问令牌。我设置了我的配置,创建了 msal 对象,定义了重定向承诺,然后使用适当的用户范围调用 loginRedirect。页面正确重定向。
但是,在我登录后,tokenResponse 返回为空。我曾尝试更改权限和范围,但它总是返回为空。如何让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);
Run Code Online (Sandbox Code Playgroud)
编辑:
我也曾尝试authority: `https://<tenant-name>.b2clogin.com/<tenant-name>.onmicrosoft.com/<policy-name>并https://login.microsoftonline.com/tfp/{tenant}.onmicrosoft.com/B2C_1_SiupIn在权威msalConfig的对象,以及scopes: ['openid']在loginRequest。当我使用它时,当我尝试登录时,我在浏览器中收到以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
您设置重定向流程的方式似乎是正确的。您首先必须调用handleRedirectPromise()(注册它),然后调用loginRedirect(). 页面加载时handleRedirectPromise()将返回null,登录后应返回令牌。
但是,您的配置存在问题。
knownAuthority,例如: auth: {
clientId: 'xxxx-xx-xx-xx-xxxxx',
authority: 'https://<tenant-name>.b2clogin.com/<tenant-name>.onmicrosoft.com/<policy-name>',
knownAuthorities: ['<your-tenant-name>.b2clogin.com']
redirectUri: 'https://localhost:4200'
},
Run Code Online (Sandbox Code Playgroud)
User.Read是 MS Graph API 范围。您不能将其用于 B2C。仅允许使用OIDCopenid范围,即使用它。请参阅此了解更多信息。
问题出在我的角度应用程序上。我让我的应用程序将基本 url 重定向到我的 /home 路由。因此,每当您打开基本路线时,应用程序都会被重定向。从该路由发出请求。我将 /home 路由的重定向 URI 添加到我的 AAD 应用程序注册中,在我的 b2c 配置中注释掉重定向 URI,并将 navigatorToLoginRequestUrl 设置为 true。
| 归档时间: |
|
| 查看次数: |
4840 次 |
| 最近记录: |