This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
When an app needs to access an Azure resource like Azure Storage, Azure Key Vault, or Azure AI services, the app must be authenticated to Azure. This requirement is true for all apps, whether they're deployed to Azure, deployed on-premises, or under development on a local developer workstation. This article describes the recommended approaches to authenticate an app to Azure when you use the Azure SDK for Python.
Use token-based authentication rather than connection strings for your apps when they authenticate to Azure resources. TheAzure Identity client library for Python provides classes that support token-based authentication and allow apps to seamlessly authenticate to Azure resources whether the app is in local development, deployed to Azure, or deployed to an on-premises server.
The specific type of token-based authentication an app uses to authenticate to Azure resources depends on where the app is being run. The types of token-based authentication are shown in the following diagram.
TheDefaultAzureCredential class provided by the Azure Identity client library allows apps to use different authentication methods depending on the environment in which they're run. In this way, apps can be promoted from local development to test environments to production without code changes.
You configure the appropriate authentication method for each environment, andDefaultAzureCredential
automatically detects and uses that authentication method. The use ofDefaultAzureCredential
is preferred over manually coding conditional logic or feature flags to use different authentication methods in different environments.
Details about using theDefaultAzureCredential
class are discussed in the sectionUse DefaultAzureCredential in an application.
Use token-based authentication instead of using connection strings when you build apps for Azure. Token-based authentication offers the following advantages over authenticating with connection strings:
Limit the use of connection strings to initial proof-of-concept apps or development prototypes that don't access production or sensitive data. Otherwise, the token-based authentication classes available in the Azure Identity client library are always preferred when they're authenticating to Azure resources.
When you're hosting in a server environment, each app is assigned a uniqueapplication identity per environment where the app runs. In Azure, an application identity is represented by aservice principal. This special type of security principal identifies and authenticates apps to Azure. The type of service principal to use for your app depends on where your app is running:
Authentication method | Description |
---|---|
Apps hosted in Azure | Apps hosted in Azure should use amanaged identity service principal. Managed identities are designed to represent the identity of an app hosted in Azure and can only be used with Azure-hosted apps. For example, a Django web app hosted in Azure App Service would be assigned a managed identity. The managed identity assigned to the app would then be used to authenticate the app to other Azure services. Apps running in Azure Kubernetes Service (AKS) can use a Workload identity credential. This credential is based on a managed identity that has a trust relationship with an AKS service account. , |
Apps hosted outside of Azure (for example, on-premises apps) | Apps hosted outside of Azure (for example, on-premises apps) that need to connect to Azure services should use anapplication service principal. An application service principal represents the identity of the app in Azure and is created through the application registration process. For example, consider a Django web app hosted on-premises that makes use of Azure Blob Storage. You would create an application service principal for the app by using the app registration process. The AZURE_CLIENT_ID ,AZURE_TENANT_ID , andAZURE_CLIENT_SECRET would all be stored as environment variables to be read by the application at runtime and allow the app to authenticate to Azure by using the application service principal. |
When an app runs on a developer's workstation during local development, it still must authenticate to any Azure services used by the app. There are two main strategies for authenticating apps to Azure during local development:
Authentication method | Description |
---|---|
Create dedicated application service principal objects to be used during local development. | In this method, dedicatedapplication service principal objects are set up by using the app registration process for use during local development. The identity of the service principal is then stored as environment variables to be accessed by the app when it's run in local development. This method allows you to assign the specific resource permissions needed by the app to the service principal objects used by developers during local development. This practice makes sure the application only has access to the specific resources it needs and replicates the permissions the app will have in production. The downside of this approach is the need to create separate service principal objects for each developer who works on an application. |
Authenticate the app to Azure by using the developer's credentials during local development. | In this method, a developer must be signed in to Azure from the Azure CLI, Azure PowerShell, or Azure Developer CLI on their local workstation. The application then can access the developer's credentials from the credential store and use those credentials to access Azure resources from the app. This method has the advantage of easier setup because a developer only needs to sign in to their Azure account through one of the aforementioned developer tools. The disadvantage of this approach is that the developer's account likely has more permissions than required by the application. As a result, the application doesn't accurately replicate the permissions it will run with in production. |
DefaultAzureCredential is an opinionated, ordered sequence of mechanisms for authenticating to Microsoft Entra ID. Each authentication mechanism is a class that implements theTokenCredential protocol and is known as acredential. At runtime,DefaultAzureCredential
attempts to authenticate using the first credential. If that credential fails to acquire an access token, the next credential in the sequence is attempted, and so on, until an access token is successfully obtained. In this way, your app can use different credentials in different environments without writing environment-specific code.
To useDefaultAzureCredential
in a Python app, add theazure-identity package to your application.
pip install azure-identity
Azure services are accessed using specialized client classes from the various Azure SDK client libraries. The following code example shows how to instantiate aDefaultAzureCredential
object and use it with an Azure SDK client class. In this case, it's aBlobServiceClient
object used to access Azure Blob Storage.
from azure.identity import DefaultAzureCredentialfrom azure.storage.blob import BlobServiceClient# Acquire a credential objectcredential = DefaultAzureCredential()blob_service_client = BlobServiceClient( account_url="https://<my_account_name>.blob.core.windows.net", credential=credential)
When the preceding code runs on your local development workstation, it looks in the environment variables for an application service principal or at locally installed developer tools, such as the Azure CLI, for a set of developer credentials. Either approach can be used to authenticate the app to Azure resources during local development.
When deployed to Azure, this same code can also authenticate your app to Azure resources.DefaultAzureCredential
can retrieve environment settings and managed identity configurations to authenticate to Azure services automatically.
Was this page helpful?