3
votes

I downloaded the example from GitHub to experiment with Azure AD B2C https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi

I have reconfigured this Web App for my AADB2C and all works well.

To also experiment with MSAL.js, I added a new page that implements the get access token for logged in user. User is logged in using server side code. However, I was not able to get cached user session for the logged user as it seems MSAL.js dose not know the user already logged in from the server side code and vise-versa.

here is the code I used to get the logged user session and try to get token silently.

if (msalInstance.getAccount()) {
    var tokenRequest = {
        scopes: ["user.read", "mail.send"]
    };
    msalInstance.acquireTokenSilent(tokenRequest)
        .then(response => {
            // get access token from response
            // response.accessToken
        })
        .catch(err => {
            // could also check if err instance of InteractionRequiredAuthError if you can import the class.
            if (err.name === "InteractionRequiredAuthError") {
                return msalInstance.acquireTokenPopup(tokenRequest)
                    .then(response => {
                        // get access token from response
                        // response.accessToken
                    })
                    .catch(err => {
                        // handle error
                    });
            }
        });
} else {
    // user is not logged in, you will need to log them in to acquire a token
}

the msalInstance.getAccount() function will always return null even the user has already logged in using the Microsoft.Identity Code (MSAL C# lib).

If anyone could if it is possible to get access token silently if the user is logged in using service side code.

1
I don't see how this question has anything to do with C#MindSwipe
Thank you I removed the c# tagging.Xin Jin
You can use msal.js to login directly, why did you login using msal c# lib?Tony Ju

1 Answers

0
votes

Had the same issue. Strangely the developers Msal.js have security concerns when your .js-application adopts the session started in your .net-application. Although client side is not the right place to put that kind of security mechanism. Here is where i found that info: related issue on github

You can adopt the .net-started session in your .js-application with msal.js by adding session id or login_hint to acquireTokenSilent(). More info can be found here: official msal.js documentation