question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Acquire access token from API using AzureB2C

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Core Library Version

2.20.0

Wrapper Library

MSAL Angular (@azure/msal-angular)

Wrapper Library Version

2.0.6

Public or Confidential Client?

Public

Description

I am trying to implement a signup-signin process to allow external users into our application using AzureB2C. My angular application and API are currently protected using Azure AD authentication with the PKCE flow. I have added a service principal for the API registration from the Azure AD tenant in the Azure B2C tenant. I have added the necessary scopes in the AzureB2C client app registration. However, when I try to login using loginRedirect() I only get back an Id Token and Refresh Token and am unable to access the API. I am not super familiar with creating a multi-tenant application, so I am unsure if my approach is correct. Any help would be greatly appreciated!

MSAL Configuration

[
    {
        "AzureAd" : {
            "config" : {
                "auth" : {
                    "authority" : "https://login.microsoftonline.com/{tenantId}",
                    "redirectUri" : "http://localhost:4200"
                },
                "cache" : {
                    "cahceLocation" : "localStorage",
                    "storeAuthStateInCookie" : false
                }
            },
            "guard" : {
                "interactionType" : "popup",
                "authRequest" : {
                    "scopes" : ["https://{azureADTenantdomain}.onmicrosoft.com/{apiClientId}/access_as_user"]
                }
            },
            "interceptor" : {
                "protectedResourceMap" : [
                    ["https://localhost:5445/api/*", ["https://{azureADTenantdomain}.onmicrosoft.com/{apiClientId}/access_as_user"]],
                    ["https://graph.microsoft.com/v1.0/", ["User.Read", "User.ReadBasic.All"]]
                ]
            }
        } 
    }, 
    {
        "AzureB2C": {
            "config" : {
                "auth" : {
                    "authority" : "https://{b2cTenantDomain}.b2clogin.com/{b2cTenantDomain}.onmicrosoft.com/B2C_1_{susi_flow}",
                    "knownAuthorities" : ["{b2cTenantDomain}.b2clogin.com", "{azureADTenantdomain}.onmicrosoft.com"],
                    "redirectUri" : "http://localhost:4200"
                },
                "cache" : {
                    "cahceLocation" : "localStorage",
                    "storeAuthStateInCookie" : false
                }
            },
            "guard" : {
                "interactionType" : "popup",
                "authRequest" : {
                    "scopes" : ["https://{azureADTenantdomain}.onmicrosoft.com/{apiClientId}/access_as_user"]
                }
            },
            "interceptor" : {
                "protectedResourceMap" : [
                    ["https://localhost:5445/api/*", ["https://{azureADTenantdomain}.onmicrosoft.com/{apiClientId}/access_as_user"]]
                ]
            }
        }
    }
]

Relevant Code Snippets

No response

Identity Provider

Azure B2C Basic Policy

Source

External (Customer)

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
AniTang99commented, Nov 11, 2022

@derisen I was able to add another authentication scheme in addition to the Azure AD scheme and now the API correctly checks for either token! Thank you for your help.

0reactions
AniTang99commented, Nov 11, 2022

@derisen After cleaning the slate and rewriting the login process step by step, I am now successfully able to get an access token! However, now my API is rejecting the access token due to an invalid signature. The API successfully accepts tokens when logging in as a user from the Azure AD tenant so do I need to somehow match the signature of the tokens, or should my API be configured somehow to handle both the Azure AD and B2C tokens? Thank you for your guidance!!

API is an ASP.NET Core web api

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request an access token - Azure Active Directory B2C
An access token contains claims that you can use in Azure Active Directory B2C (Azure AD B2C) to identify the granted permissions to...
Read more >
Getting an access token in Azure AD B2C - Medium
I've defined my endpoint (“tasks-api”) at the top and added two scopes. I have a test application called “Test_B2C”. I've added the “tasks.read”...
Read more >
Obtaining an Access Token from Azure B2C using OAuth2.0 ...
Select Authorization Code (With PKCE) as the Grant Type. · Enter the Redirect Uri as the Callback URL. · Enter the Authorize Url...
Read more >
Request Access Token in Postman for Azure AD B2C
Postman · Select Policy Keys and then select Add. · For Options, choose Generate. · In Name, enter TokenEncryptionKeyContainer. The prefix B2C_1A_ ...
Read more >
Set Up Access Token Authorization with Azure ADB2C
Access tokens are purposefully short-lived to limit potential abuse if a malicious party gets a hold of one. By default, the access tokens...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found