0
votes

I am having a .net core web api which is secured with Azure AD B2C and I am trying to access the api using react js msal library.

new Msal.UserAgentApplication(config.applicationId, authority, authCallback, { logger: logger, cacheLocation: config.cacheLocation, postLogoutRedirectUri: config.postLogoutRedirectUri, redirectUri: config.redirectUri } );

How to call a web api by passing the JWT token by using react js

1

1 Answers

0
votes

It looks like you are using MSAL js v0.2.3 or v0.2.4, the new MSAL v1.0.0 release takes a configuration object in the constructor for the UserAgentApplication. Make sure to refer to the documentation for correct usage for your version: https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki

Regardless - you'll need to initiate a login after initialization by calling loginPopup or loginRedirect on the MSAL app. After successful credentials are entered and you return to your web app, you can call MSAL's getUser() ( or getAccount() if using v1.0.0 )

After that, you'll need to get an access token by calling acquireTokenSilent(scopes), where the scopes array is defined by your B2C application.
( In v1.0.0 you'd pass a parameter object like this: { scopes: [scopes] } )

Once you have an access token - you'll need to pass it as the Authorization header on your REST request, and then validate it in your WebAPI by calling back to B2C before processing the request. This is an example of the validation process: https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapi-manual-jwt-validation/