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.

*Credential.getToken support for clientID and tenantId

See original GitHub issue

In C# if you are still using AzureServiceTokenProvider.GetAccessTokenAsync(…) and pass in a resource (clientID) and a tenantID, it allows you to directly call the Azure application. I do not see an equivalent case in @azure/identity.

var clientId = "{GUID}";
var tenantID = "{GUID}";
var identity = "RunAs=App";
 
var azureServiceTokenProvider = new AzureServiceTokenProvider(identity);
var accessToken = azureServiceTokenProvider.GetAccessTokenAsync(clientId, tenantID).GetAwaiter().GetResult();
Console.WriteLine(accessToken);

The scenario is we are using a system managed identity for an Azure Function and trying to get a token from an OAuth2 endpoint that represents APIM.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
normand-MSFTcommented, Jun 2, 2021

Passing in the clientid of the app worked. The confusing part and why we didn’t look at that before, even though from your perspective the user assigned MSI and app registration clientID are the same, is the documentation. That implies we need to create a standalone managed identity azure resource. We just needed to also use a scope of “api://{guid}/.default” though (a different issue) why we are limited to using .default.

The client id to authenticate for a user assigned managed identity. More information on user assigned managed identities can be found here: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview#how-a-user-assigned-managed-identity-works-with-an-azure-vm

1reaction
ramya-rao-acommented, May 12, 2021

Thanks for sharing the C# code snippet that you are trying to convert

I looked into the implementation of AzureServiceTokenProvider and it looks like it uses the “connectionString” passed to the constructor to determine how to do authentication. The “connectionString” is a list of key value pair and when you pass “RunAs=App”, it uses the Managed Identity.

This way of using connection string like input to determine behavior is not encouraged anymore due to the nature of it being prone to human error when the string is being constructed. I am not aware of what the counterpart for this is in the newer .NET libraries, but we can share the recommended practice in JavaScript/TypeScript.

RunAs=App should translate to the system managed identity for the Azure Function. Its equivalent should be ManagedIdentityCredential without parameters.

You are right in determining that using the ManagedIdentityCredential without parameters as the path forward. You can then await on the getToken() call on the credential to get your token. Can you share what issues/errors you found when doing so?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use the Azure Identity library to get an access token for ...
A simple way to get the access token and token credential is to use the DefaultAzureCredential class that is provided by the Azure...
Read more >
Azure Identity client library for .NET
The Azure Identity library provides Azure Active Directory token authentication support across the Azure SDK. It provides a set of TokenCredential ...
Read more >
DefaultAzureCredential: Unifying How We Get Azure AD Token
The Azure Identity library provides Azure Active Directory token authentication support across the Azure SDK. It provides a set of ...
Read more >
Get Service Principal token with curl | azure-notes
Directory ID, Tenant ID, 72f988bf-86f1-41af-91ab-2d7cd011db47 ; Application ID, Client ID, b0e35524-7612-42bd-ae7b-d7e74accb8cc ; Key, Client Secret, kY/ ...
Read more >
c# - Get Token from Azure using AAD App (ClientID, TenantID ...
You must use ClientAssertionCertificate instead of ClientCredential X509Certificate2 cert = ReadCertificateFromStore(config.
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