For a test scenario I want to create an access token for an AAD Application with the APPLICATION_ID as aud claim.
I created a correct token using Azure CLI (after I authorized the Azure CLI Application using Expose an API view).
$ az login -u <USERNAME> --allow-no-subscription
$ az account get-access-token --tenant <TENANT_ID> --resource <APPLICATION_ID>
I tried something similar in Node.js using @azure/ms-rest-nodeauth.
async function getToken(): Promise<string> {
const credentials = await msRestNodeAuth.loginWithUsernamePassword(
USERNAME,
PASSWORD, {
domain: TENANT_ID,
tokenAudience: APPLICATION_ID,
},
);
return (await credentials.getToken()).accessToken;
}
The code returns a Bearer token, but I got 401 when using it. It turned out, that the aud claim was set in a different way than the Azure CLI does. The Node code prefixes the value with spn:, which is a documented behavior for the SAML protocol (see Audience section).
I tried also to use the ../oauth2/v2.0/token REST endpoint with grant_type=password, but wasn't able to get any token.
How can I create an user token with exactly APPLICATION_ID as aud claim?
grant_type=passwordapproach. I gotAADSTS50126: Error validating credentials due to invalid username or password.as error message.