angular - 如何使用 loginRedirect 而不是 loginPopup?

我正在尝试使用 Azure AD B2C 为 Angular 2+ 应用程序实现身份验证。发现了这个很棒的 example 。但是,该示例在弹出窗口中进行身份验证。我更喜欢使用重定向而不是弹出窗口。进行popup认证的MSAL Service代码如下:

import { Injectable } from '@angular/core';
declare var bootbox: any;
declare var Msal:any;
@Injectable()
export class MsalService {

    access_token: string;

    tenantConfig = {
        tenant: "fabrikamb2c.onmicrosoft.com",
        clientID: '90c0fe63-bcf2-44d5-8fb7-b8bbc0b29dc6',
        signUpSignInPolicy: "b2c_1_susi",
        b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read"]
    };

    // Configure the authority for Azure AD B2C

    authority = "https://login.microsoftonline.com/tfp/" + this.tenantConfig.tenant + "/" + this.tenantConfig.signUpSignInPolicy;

    /*
     * B2C SignIn SignUp Policy Configuration
     */
    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;
        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);
        });
    }

    logout(): void {
        this.clientApplication.logout();
    };

    isOnline(): boolean {
        return this.clientApplication.getUser() != null; 
    };
}

如何使用 loginRedirect 而不是 loginPopup 更改此代码以执行相同的操作?

最佳答案

then 中取出 .loginPopup(blah).then()... 中的代码并将其放入 // Called after loginRedirect or acquireTokenPopup 所在的块中。