Client Credentials Authentication

The Client Credentials flow is designed for service-to-service authentication, where an application needs to access Azure resources without user interaction. This is achieved by using a client ID and a client secret (or certificate) to obtain an access token.

When to Use

How it Works

The process involves:

  1. The application presents its client ID and either a client secret or a certificate to Azure Active Directory (now Microsoft Entra ID).
  2. Microsoft Entra ID validates these credentials.
  3. If valid, Microsoft Entra ID issues an access token that the application can use to authenticate to Azure Resource Manager or other Azure services.

Implementation Example (Python)

Here's a basic example of how to use the azure-identity library in Python to acquire a token using client credentials:


from azure.identity import ClientSecretCredential
from azure.core.exceptions import ClientAuthenticationError

# Replace with your actual tenant ID, client ID, and client secret
tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"

try:
    credential = ClientSecretCredential(tenant_id, client_id, client_secret)

    # The scope defines the Azure resource for which the token is requested
    # For example, to access Azure Resource Manager:
    scope = "https://management.azure.com/.default"

    # Get the token
    token = credential.get_token(scope)

    print(f"Successfully obtained access token:")
    print(f"Token Type: {token.token_type}")
    print(f"Expires On: {token.expires_on}")
    # In a real application, you would use token.token to make authenticated calls
    # print(f"Access Token: {token.token[:30]}...") # Print first 30 chars for brevity

except ClientAuthenticationError as e:
    print(f"Authentication failed: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
        

Important: Never hardcode client secrets directly in your code. Use environment variables, Azure Key Vault, or other secure secret management solutions.

ClientSecretCredential

This class in the azure.identity library facilitates the client secret authentication flow.

Class: ClientSecretCredential

Parameter Type Description Required
tenant_id str The Azure AD tenant ID. Yes
client_id str The application's client ID. Yes
client_secret str The client secret value. Yes
http_client azure.core.pipeline.PipelineClient (optional) An optional HTTP client to use for requests. No
claims_challenge str (optional) A claims challenge from a prior authentication attempt. No
authority str (optional) The authority to use for authentication. Defaults to https://login.microsoftonline.com/{tenant_id}. No

Client Certificate Authentication

For enhanced security, you can use a client certificate instead of a client secret. The ClientCertificateCredential class supports this.

Class: ClientCertificateCredential

Parameter Type Description Required
tenant_id str The Azure AD tenant ID. Yes
client_id str The application's client ID. Yes
certificate_path str Path to the certificate file (e.g., .pem, .pfx). Yes
certificate_password str (optional) Password for the certificate file, if it's protected. No
http_client azure.core.pipeline.PipelineClient (optional) An optional HTTP client. No
claims_challenge str (optional) A claims challenge. No
authority str (optional) The authority URL. No

Refer to the official Azure Identity for Python documentation for more details and advanced configurations.