如何从缓存位置检索msal对象:本地存储?

时间:2018-11-18 17:38:00

标签: node.js local-storage azure-ad-b2c msal msal.js

首先,关于Node.js / msal / azure b2c,我还不属于新手,我正在尝试了解流程。 我从这里的示例开始:https://azure.microsoft.com/en-us/resources/samples/active-directory-b2c-javascript-msal-singlepageapp/ 我在Nodejs应用程序中将msal.js与azure ad b2c一起使用。通过登录策略登录后,我将用户重定向到具有其他策略的其他页面。

// index.html

var clientApplication = new Msal.UserAgentApplication(applicationConfig.clientID, applicationConfig.authority, authCallback, { logger: logger, cacheLocation: 'localStorage' });
            function authCallback(errorDesc, token, error, tokenType) {
                if (token) {
                    logMessage(token + ":" + token);
                }
                else {
                    logMessage(error + ":" + errorDesc);
                }
            }

这是index.html中我的onclick登录功能。 “ test(accessToken)”方法将重定向到后端节点js路由,在该路由中我将访问令牌存储在会话变量中,并且该方法呈现到另一个页面(test.ejs),该页面存储了我的其他b2c策略。

function login() {

        clientApplication.loginPopup(applicationConfig.b2cScopes).then(function (idToken) {
            clientApplication.acquireTokenSilent(applicationConfig.b2cScopes).then(function (accessToken) {            
                test(accessToken);
            }, function (error) {
                clientApplication.acquireTokenPopup(applicationConfig.b2cScopes).then(function (accessToken) {
                    updateUI();
                }, function (error) {
                    logMessage("Error acquiring the popup:\n" + error);
                });
            })
        }, function (error) {
            logMessage("Error during login:\n" + error);
        });

    }

现在我的问题是如何在另一个视图(test.ejs)中检索clientApplication Msal.UserAgentApplication对象的当前状态以执行以下操作:

clientApplication.acquireTokenSilent(applicationConfig.b2cScopes).then(function (accessToken) {
                logMessage(accessToken);
            }

1 个答案:

答案 0 :(得分:0)

我相信您基本上应该在代码中的同一位置处理所有策略(在我看来,这实际上是登录登录并忘记密码),否则无论如何您都将被重定向到Microsoft网站。

constructor(private apiService: ApiService, private backendRoutes: BackendRoutes) {
    this._authority = `https://login.microsoftonline.com/tfp/${environment.tenant}/${environment.signUpSignInPolicy}`;

    this._clientApplication =
        new Msal.UserAgentApplication(
            environment.clientID,
            this._authority,
            this.msalHandler,
            {
                cacheLocation: 'localStorage',
                redirectUri: window.location.origin
            });
}

msalHandler(errorDesc: any, token: any, error: any, tokenType: any) {
    let userAgent: Msal.UserAgentApplication = <any>(this);
    if (errorDesc.indexOf("AADB2C90118") > -1) {
        //Forgotten password
        userAgent.authority = `https://login.microsoftonline.com/tfp/${environment.tenant}/${environment.passResetPolicy}`;
        userAgent.loginRedirect(environment.b2cScopes);

    } else if (errorDesc.indexOf("AADB2C90077") > -1) {
        //Expired Token, function call from interceptor with proper context
        this.logout();
    }
}