Authenticate with user credentials for Python applications on Azure
This article describes methods involving user credentials for authenticating applications with Azure services.
If you haven't already, review the Authentication Overview for important details that apply to all authentication methods, namely assigning application identity, granting permissions to an identity, and when authentication and authorization occur when using Azure libraries.
Interactive browser authentication
This method uses InteractiveBrowserCredential, which is described in Azure Authentication in Python development environments.
Device code authentication
This method uses DeviceCodeCredential, which is described in Azure Authentication in Python development environments.
Authentication with a username and password
This method authenticates an application using previous-collected credentials and the UsernamePasswordCredential object.
We discourage using this method of authentication because it's less secure than other flows. Also, this method is not interactive and is therefore not compatible with any form of multi-factor authentication or consent prompting. The application must already have consent from the user or a directory administrator.
Furthermore, this method authenticates only work and school accounts; Microsoft accounts are not supported. For more information, see Sign up your organization to use Azure Active Directory.
# Show Azure subscription information
import os
from azure.mgmt.resource import SubscriptionClient
from azure.identity import UsernamePasswordCredential
# Retrieve the information necessary for the credentials, which are assumed to
# be in environment variables for the purpose of this example.
client_id = os.environ["AZURE_CLIENT_ID"]
tenant_id = os.environ["AZURE_TENANT_ID"]
username = os.environ["AZURE_USERNAME"]
password = os.environ["AZURE_PASSWORD"]
credential = UsernamePasswordCredential(client_id=client_id, tenant_id = tenant_id,
username = username, password = password)
subscription_client = SubscriptionClient(credential)
subscription = next(subscription_client.subscriptions.list())
print(subscription.subscription_id)
See also
- Authentication overview
- Configure your local Python dev environment for Azure
- How to authenticate and authorize Python applications on Azure
- How to assign role permissions
- Example: Provision a resource group
- Example: Provision and use Azure Storage
- Example: Provision a web app and deploy code
- Example: Provision and use a MySQL database
- Example: Provision a virtual machine
- Use Azure Managed Disks with virtual machines
- Complete a short survey about the Azure SDK for Python