how to retrieve the msal object from cache location : local storage?












1















First things first, I am below a novice level when it comes to Nodejs/msal/azure b2c and I am trying to understand the flow.
I started off with this sample here: https://azure.microsoft.com/en-us/resources/samples/active-directory-b2c-javascript-msal-singlepageapp/
I am using msal.js with azure ad b2c in a Nodejs application. I am redirecting the user after signing in via sign-in policy to a different page where I have my other policies.



//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);
}
}


This is my onclick login function in the index.html. The 'test(accessToken)' method does the redirection to the backend node js routes where I store the accesstoken in a session variable and the method renders to a different page(test.ejs) where my other b2c policies are stored.



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);
});

}


Now my question is how can I retrieve the current state of the clientApplication Msal.UserAgentApplication object in my other view(test.ejs) to do something like this :



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









share|improve this question





























    1















    First things first, I am below a novice level when it comes to Nodejs/msal/azure b2c and I am trying to understand the flow.
    I started off with this sample here: https://azure.microsoft.com/en-us/resources/samples/active-directory-b2c-javascript-msal-singlepageapp/
    I am using msal.js with azure ad b2c in a Nodejs application. I am redirecting the user after signing in via sign-in policy to a different page where I have my other policies.



    //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);
    }
    }


    This is my onclick login function in the index.html. The 'test(accessToken)' method does the redirection to the backend node js routes where I store the accesstoken in a session variable and the method renders to a different page(test.ejs) where my other b2c policies are stored.



    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);
    });

    }


    Now my question is how can I retrieve the current state of the clientApplication Msal.UserAgentApplication object in my other view(test.ejs) to do something like this :



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









    share|improve this question



























      1












      1








      1








      First things first, I am below a novice level when it comes to Nodejs/msal/azure b2c and I am trying to understand the flow.
      I started off with this sample here: https://azure.microsoft.com/en-us/resources/samples/active-directory-b2c-javascript-msal-singlepageapp/
      I am using msal.js with azure ad b2c in a Nodejs application. I am redirecting the user after signing in via sign-in policy to a different page where I have my other policies.



      //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);
      }
      }


      This is my onclick login function in the index.html. The 'test(accessToken)' method does the redirection to the backend node js routes where I store the accesstoken in a session variable and the method renders to a different page(test.ejs) where my other b2c policies are stored.



      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);
      });

      }


      Now my question is how can I retrieve the current state of the clientApplication Msal.UserAgentApplication object in my other view(test.ejs) to do something like this :



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









      share|improve this question
















      First things first, I am below a novice level when it comes to Nodejs/msal/azure b2c and I am trying to understand the flow.
      I started off with this sample here: https://azure.microsoft.com/en-us/resources/samples/active-directory-b2c-javascript-msal-singlepageapp/
      I am using msal.js with azure ad b2c in a Nodejs application. I am redirecting the user after signing in via sign-in policy to a different page where I have my other policies.



      //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);
      }
      }


      This is my onclick login function in the index.html. The 'test(accessToken)' method does the redirection to the backend node js routes where I store the accesstoken in a session variable and the method renders to a different page(test.ejs) where my other b2c policies are stored.



      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);
      });

      }


      Now my question is how can I retrieve the current state of the clientApplication Msal.UserAgentApplication object in my other view(test.ejs) to do something like this :



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






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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 19:17









      ramana vv

      830519




      830519










      asked Nov 18 '18 at 17:37









      Raunak DeshmukhRaunak Deshmukh

      288




      288
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I believe you should basically handle all your policies(which to me actually are signuop signin and forgot password) in the same place in your code as you ll be redirected to microsoft website anyways.



          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();
          }
          }





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363718%2fhow-to-retrieve-the-msal-object-from-cache-location-local-storage%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I believe you should basically handle all your policies(which to me actually are signuop signin and forgot password) in the same place in your code as you ll be redirected to microsoft website anyways.



            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();
            }
            }





            share|improve this answer






























              0














              I believe you should basically handle all your policies(which to me actually are signuop signin and forgot password) in the same place in your code as you ll be redirected to microsoft website anyways.



              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();
              }
              }





              share|improve this answer




























                0












                0








                0







                I believe you should basically handle all your policies(which to me actually are signuop signin and forgot password) in the same place in your code as you ll be redirected to microsoft website anyways.



                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();
                }
                }





                share|improve this answer















                I believe you should basically handle all your policies(which to me actually are signuop signin and forgot password) in the same place in your code as you ll be redirected to microsoft website anyways.



                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();
                }
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 2 '18 at 5:46

























                answered Dec 2 '18 at 5:32









                Lokman ErsoyLokman Ersoy

                1318




                1318
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363718%2fhow-to-retrieve-the-msal-object-from-cache-location-local-storage%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()