简体   繁体   中英

Generating access Token with loginRedirect in Azure AD B2C portal

I am generating token using Azure AD B2C Portal. When I am using loginPopup method then I can generate Token successfully but got undefined while using loginRedirect.

here's the code:

clientApplication = new Msal.UserAgentApplication(
        this.tenantConfig.clientID, this.authority, 
        function (errorDesc: any, token: any, error: any, tokenType: any) {
            // Called after loginRedirect or acquireTokenPopup
        }
);

public login(): void {
       var _this = this;
       // loginRedirect loginPopup
        this.clientApplication.loginPopup(this.tenantConfig.b2cScopes).then(function (idToken: any) {
            _this.clientApplication.acquireTokenSilent(_this.tenantConfig.b2cScopes).then(
                function (accessToken: any) {
                    _this.access_token = accessToken;
                }, function (error: any) {
                    _this.clientApplication.acquireTokenPopup(_this.tenantConfig.b2cScopes).then(
                        function (accessToken: any) {
                            _this.access_token = accessToken;
                        }, function (error: any) {
                            bootbox.alert("Error acquiring the popup:\n" + error);
                        });
                })
        }, function (error: any) {
            bootbox.alert("Error during login:\n" + error);
        });
        console.log(`access token service file ${_this.access_token}`);
    }

Please let me know what mistake I am doing? is it Scope issue OR callback method issue?

I found the reference . By this I was able to achieve access token. you will get it in sessionStorage.getItem('msal.idtoken') .

For loginRedirect you add your handler code of the response in // Called after loginRedirect or acquireTokenPopup block.

clientApplication = new Msal.UserAgentApplication(
        this.tenantConfig.clientID, this.authority, 
        function (errorDesc: any, token: any, error: any, tokenType: any) {
            // Called after loginRedirect or acquireTokenPopup
            // perform your logic HERE! :)
        }
);

public login(){
    this.clientApplication.loginRedirect(this.tenantConfig.b2cScopes);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM