node.js - 如何从缓存位置:本地存储中检索msal对象?

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

首先,当涉及到Nodejs / 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);
            }

最佳答案

我相信您基本上应该在代码中的同一位置处理所有策略(对我来说实际上是登录名和忘记密码),否则无论如何您将被重定向到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();
    }
}

关于node.js - 如何从缓存位置:本地存储中检索msal对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53363718/

相关文章:

node.js - 为什么在Sequelize 中关联belongsTo 会给我这个错误?

javascript - socket.io 的好的初学者教程?

azure-ad-b2c - msal.js - 注销而不重定向

azure-active-directory - Azure AD B2C 中的刷新 token 吊销

javascript - 无法使用 alt 操作触发 alt 存储 (Flux)

Javascript 回调和堆栈溢出

javascript - Safari IOS 10 上的错误 : The quota has been exceeded.

javascript - PhantomJS- 默认使用 LocalStorage 打开页面

javascript - 如何删除本地存储中的数据

azure - 如何在 Azure AD B2C 租户中注册 Zendesk 应用程序(基于 SAML)以执行 SSO?