Azure ad b2c msal.js 2.0 LoginDirect后的tokenResponse null

Azure ad b2c msal.js 2.0 LoginDirect后的tokenResponse null,azure-ad-b2c,msal,msal.js,Azure Ad B2c,Msal,Msal.js,我正在开发一个Angular 10应用程序,它利用Azure B2C进行策略和用户管理。我在Azure Active Directory中将我的应用注册设置为单页应用,但未选中隐式选项。我正在使用msal.js2.0@azure/msal browser登录B2C,并使用代码流检索id和访问令牌。我设置了配置,创建了msal对象,定义了重定向承诺,然后使用适当的用户范围调用LoginDirect。页面正确重定向 但是,在我登录后,tokenResponse返回为null。我尝试过修改权限和作用域

我正在开发一个Angular 10应用程序,它利用Azure B2C进行策略和用户管理。我在Azure Active Directory中将我的应用注册设置为单页应用,但未选中隐式选项。我正在使用msal.js2.0
@azure/msal browser
登录B2C,并使用代码流检索id和访问令牌。我设置了配置,创建了msal对象,定义了重定向承诺,然后使用适当的用户范围调用LoginDirect。页面正确重定向

但是,在我登录后,tokenResponse返回为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);
编辑:

我还尝试了
权限:`https://.b2clogin.com/.onmicrosoft.com/
https://login.microsoftonline.com/tfp/{tenant}.onmicrosoft.com/B2C_1_SiupIn
用于
msalConfig
对象中的权限以及
loginRequest
中的
作用域:['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.
zone evergreen.js:1068获取https://login.microsoftonline.com/common/discovery/instance?api-版本=1.1和授权\u端点=https://.b2clogin.com/.onmicrosoft.com/b2c_1_defaultsigninsignup/oauth2/v2.0/authorize 400(错误请求)
core.js:4197错误:未捕获(承诺中):ClientAuthError:endpoints\u resolution\u错误:错误:无法解析端点。请检查网络并重试。详细信息:ClientConfigurationError:不受信任的权限:提供的权限不是受信任的权限。请在knownAuthorities配置参数中包含此权限。
ClientAuthError:endpoints\u resolution\u错误:错误:无法解析终结点。请检查网络并重试。详细信息:ClientConfigurationError:不受信任的权限:提供的权限不是受信任的权限。请在knownAuthorities配置参数中包含此权限。

设置重定向流的方式似乎是正确的。首先必须调用
handleRedirectPromise()
(注册它),然后调用
loginRedirect()
。在页面加载时,handleRedirectPromise()将返回
null
,登录后应返回令牌

但是,您的配置存在问题

  • 您需要将您的域指定为
    知识权限
    ,如:
  • auth:{
    客户ID:'xxxx-xx-xx-xx-xxxxx',
    当局:'https://.b2clogin.com/.onmicrosoft.com/', 
    知识权限:['.b2clogin.com']
    重定向URI:'https://localhost:4200'
    },
    
  • User.Read
    是MS Graph API作用域。您不能将其用于B2C。只允许使用OIDC作用域,即使用
    openid

  • 有关更多信息,请参阅。

    问题在于我的angular应用程序。我让我的应用程序将基本url重定向到my/home路由。因此,每当您打开基本路由时,应用程序都会被重定向。从该路径发出请求。我将/home路由的重定向uri添加到我的AAD应用程序注册中,在我的b2c配置中注释掉重定向uri,并将navigateToLoginRequestUrl设置为true。

    检查浏览器控制台中的浏览器会话存储,查看MSAL是否有错误。该权限绝对不正确。应采用“格式”https://.b2clogin.com/.onmicrosoft.com/”. 请参阅示例,AAD B2C没有“user.read”这样的范围。看到这里,你配置的是一个纯AAD流,而不是AAD B2C。所以@Jas Suri,我作为一个权威尝试了一下,我在浏览器中遇到了一个错误(我更新了我的帖子,出现了完整的错误)。我还检查了sessionStorage,它有“错误:[“endpoints\u resolution\u error”,“endpoints\u resolution\u error”]”。我还尝试使用openid和空数组[]作为作用域。我查找了使用MSAL2.0的b2c示例,发现很少。我遵循这个指南:谢谢!我取得了一些进展。我将knownAuthorities条目添加到我的msalConfig中。当我尝试登录时,不再出现错误。现在,我在sessionStorage中获得不同值的msal条目:params、origin、authority和nonce.id_标记。但是,完全重定向不会发生。看起来它试图重定向,但又重定向回来了。此外,tokenResponse仍然为空。欢迎:)只是为了确认,您是否也删除了“user.read”作用域并替换为“openid”?是的,我使用了{scopes:['openid']}。我想知道这是否与我的应用程序注册有关?我在这里注册了Msal 2.0。。可能与浏览器相关。你有一个我可以查看的公共存储库吗?或者,您可以通过电子邮件向我发送网络跟踪(我建议使用Fiddler)。此外,还有一个角度为10的AAD样本,您可以检查