0

I have a React front end, Node.js backend app with Azure SQL DB that has Azure AD auth on the React side, and managed identities in Azure. I am trying to get the access token using DefaultAzureCredential from the @azure/identity package so I can connect to the database, but it is not getting the access token. The app has been created on Azure App Services and currently being deployed even though it is in development. When I run my Node.js code I get this error:

AggregateAuthenticationError: ChainedTokenCredential authentication failed. CredentialUnavailableError: EnvironmentCredential is unavailable. No underlying credential could be used.

The message actually goes much longer than above, but that is the first part of it. My code is:

const { Connection, Request } = require("tedious");
const { DefaultAzureCredential } = require("@azure/identity");

const credential = new DefaultAzureCredential({ managedIdentityClientId: process.env.AZURE_DEV_MI_CLIENT_ID }); // user-assigned identity

// Get token for Azure SQL Database
console.log(credential)
let accessToken;
async function getAccessToken() {
    try {
    accessToken = await credential.getToken("https://database.windows.net/.default")
    console.log('accessToken ' + accessToken)
    } catch(err){
        console.log('Error! ' + err)
    }
}
getAccessToken();

I took this straight from the docs in Azure, tried a few different code configurations, but nothing seems to get the access token. I've also been double-checking the set ups in the Azure DB, App Service, and AD, and can't seem to find a solution. Hoping someone knows what is needed for this, and appreciate any ideas anyone has.

Mr. K
  • 73
  • 8

0 Answers0