Acquire an access token
A client application can request managed identities for Azure resources app-only access token for accessing a given resource. The token is based on the managed identities for Azure resources service principal. The recommended method is to use the DefaultAzureCredential.
The Azure Identity library supports a DefaultAzureCredential type. DefaultAzureCredential automatically attempts to authenticate via multiple mechanisms, including environment variables or an interactive sign-in. The credential type can be used in your development environment using your own credentials. It can also be used in your production Azure environment using a managed identity. No code changes are required when you deploy your application.
The DefaultAzureCredential attempts to authenticate via the following mechanisms, in this order, stopping when one succeeds:
- Environment – The
DefaultAzureCredentialreads account information specified via environment variables and use it to authenticate. - Managed Identity – If the application is deployed to an Azure host with Managed Identity enabled, the
DefaultAzureCredentialauthenticates with that account. - Visual Studio – If the developer authenticated via Visual Studio, the
DefaultAzureCredentialauthenticates with that account. - Azure CLI – If the developer authenticated an account via the Azure CLI
az logincommand, theDefaultAzureCredentialauthenticates with that account. Visual Studio Code users can authenticate their development environment using the Azure CLI. - Azure PowerShell – If the developer authenticated an account via the Azure PowerShell
Connect-AzAccountcommand, theDefaultAzureCredentialauthenticates with that account. - Interactive browser – If enabled, the
DefaultAzureCredentialinteractively authenticates the developer via the current system’s default browser. By default, this credential type is disabled.