gpt4 book ai didi

azure-ad-b2c - 登录重定向后 msal.js 2.0 tokenResponse null

转载 作者:行者123 更新时间:2023-12-03 23:03:33 30 4
gpt4 key购买 nike

我正在开发一个使用 Azure B2C 进行策略和用户管理的 Angular 10 应用程序。我在 Azure Active Directory 中将我的应用注册设置为单页应用,而未选中隐式选项。我正在使用 msal.js 2.0 @azure/msal-browser登录 B2C 并使用代码流检索 id 和访问 token 。我设置了我的配置,创建了 msal 对象,定义了重定向 promise ,然后使用适当的用户范围调用 loginRedirect。页面正确重定向。
但是,在我登录 tokenResponse 后返回为空。我曾尝试更改权限和范围,但它总是返回为空。我如何获得 handleRedirectPromise返回一个有效的 token 响应?
这是我的代码:

    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);
编辑:
我也试过 authority: `https://<tenant-name>.b2clogin.com/<tenant-name>.onmicrosoft.com/<policy-name>https://login.microsoftonline.com/tfp/{tenant}.onmicrosoft.com/B2C_1_SiupInmsalConfig中的权威对象以及 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.

最佳答案

您设置重定向流程的方式似乎是正确的。您首先必须调用handleRedirectPromise() (注册它),然后调用 loginRedirect() .在页面加载 handleRedirectPromise()将返回 null ,并且在登录后它应该返回 token 。
但是,您的配置存在问题。

  • 您需要将您的域指定为 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'
    },
  • User.Read是一个 MS Graph API 范围。您不能将其与 B2C 一起使用。只有 OIDC 允许范围,即使用 openid反而。

  • this更多。

    关于azure-ad-b2c - 登录重定向后 msal.js 2.0 tokenResponse null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64083852/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com