Search code examples
node.jssingle-page-applicationazure-ad-b2caccess-tokenwebapi

Azure B2C React SPA dose not providing access token


I am working on my first project of connecting Azure B2C with MERN App. I wanted to Sign In using Azure B2C and authorise my web API using the Access Token.

I configured everything and applied configurations to this sample React tutorial provide by their documentation.

The problem arises while calling the Web API. The Web API call are sending without any token. When I check the code, acquireTokenSilent function returning empty accessToken from response.

instance.acquireTokenSilent({
                scopes: protectedResources.apiHello.scopes,
                account: account
            }).then(async (response) => {
                console.log(response) 

The Request is: enter image description here

Even though I looked many forums and Microsoft technical forums, no answer is not working.

But what I noticed is, it is requesting for grant_type: authorization_code but am not seeing access token in the response. Posting here the API call, request and response.

enter image description here

enter image description here

The Response is producing id_token but not access token,

enter image description here

I gave grant permission in the SPA App permission for task.read scope. I tried everything but I am still receiving the access token as empty. How could I fix this issue?


Solution

  • I tried to reproduce the same in my environment and got below results:

    I registered one Azure AD B2C application for app1 and added scopes(task.read) as below:

    enter image description here

    Now I created one SPA registration and added API permissions by granting consent like this: enter image description here

    I created Sign up and sign in policy and ran the user flow as below:

    enter image description here

    Please Check authentication and access token and id token:

    enter image description here

    I signed in as user it gave me auth code in address bar.

    https://<tenant name >.b2clogin.com/<tenant name> .onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_susi&client_id=<app id>&nonce=defaultNonce&redirect_uri=https://jwt.ms&scope=openid%20https%3A%2F%2F<tenant name>.onmicrosoft.com tasks.read&response_type=code&prompt=login&code_challenge_method=S256&code_challenge=<challenge paramater>
    

    enter image description here

    I generated the access token via Postman with commands like this:

    POST  https://tenant.b2clogin.com/tenant.onmicrosoft.com/policy/oauth2/v2.0/token
    
    grant_type: authorization_code
    client_id: SPA_appid
    scope:  https://tenant.onmicrosoft.com/app1/task.read
    redirect_uri: redirect_uri
    code: code
    code_verifier: code_verifier
    

    Postman

    enter image description here enter image description here

    When I decode the token i getting scp in jwt .ms

    enter image description here