Skip to content

Authentication Setup

Learn how to securely authenticate your applications with Azure AI Foundry services.

Authentication Overview

Azure AI Foundry uses Azure Active Directory (Azure AD) for authentication, ensuring secure access to resources and services.

Authentication Methods

  1. Interactive Browser Authentication
  2. Best for development
  3. Uses Azure CLI
  4. Browser-based login flow
  5. Multi-factor authentication support

  6. Service Principal Authentication

  7. Recommended for production
  8. Non-interactive scenarios
  9. Application-level access
  10. Role-based access control

  11. Managed Identity

  12. Azure-hosted applications
  13. Automatic credential management
  14. Enhanced security
  15. Simplified configuration

Setting Up Authentication

1. Interactive Authentication

# Install Azure CLI
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

# Login to Azure
az login

# Set subscription
az account set --subscription <your-subscription-id>

2. Service Principal Authentication

from azure.identity import ClientSecretCredential
from azure.ai.resources import AIProjectClient
import os

def get_ai_client():
    """Initialize AI Project client with service principal authentication."""
    try:
        # Initialize the credential with service principal details
        credential = ClientSecretCredential(
            tenant_id=os.getenv("AZURE_TENANT_ID"),
            client_id=os.getenv("AZURE_CLIENT_ID"),
            client_secret=os.getenv("AZURE_CLIENT_SECRET")
        )

        # Create the AI Project client
        client = AIProjectClient(
            subscription_id=os.getenv("AZURE_SUBSCRIPTION_ID"),
            resource_group=os.getenv("AZURE_RESOURCE_GROUP"),
            credential=credential
        )

        return client
    except Exception as e:
        print(f"Error initializing AI client: {str(e)}")
        raise

# Example environment variable setup
"""
Required environment variables:
- AZURE_TENANT_ID: Your Azure AD tenant ID
- AZURE_CLIENT_ID: Your service principal client ID
- AZURE_CLIENT_SECRET: Your service principal secret
- AZURE_SUBSCRIPTION_ID: Your Azure subscription ID
- AZURE_RESOURCE_GROUP: Your resource group name
"""

3. Managed Identity Authentication

```python from azure.identity import DefaultAzureCredential from azure.ai.resources import AIProjectClient

def get_ai_client_managed_identity(): """Initialize AI Project client with managed identity authentication.""" try: # DefaultAzureCredential will use managed identity when available credential = DefaultAzureCredential()

    client = AIProjectClient(
        subscription_id=os.getenv("AZURE_SUBSCRIPTION_ID"),
        resource_group=os.getenv("AZURE_RESOURCE_GROUP"),
        credential=credential
    )

    return client
except Exception as e:
    print(f"Error initializing AI client with managed identity: {str(e)}")
    raise

Security Best Practices

  1. Credential Management ```python from azure.keyvault.secrets import SecretClient from azure.identity import DefaultAzureCredential

def get_credentials_from_keyvault(): """Retrieve credentials from Azure Key Vault.""" try: # Initialize Key Vault client credential = DefaultAzureCredential() key_vault_url = os.getenv("AZURE_KEYVAULT_URL") secret_client = SecretClient(vault_url=key_vault_url, credential=credential)

       # Retrieve secrets
       client_id = secret_client.get_secret("AI-CLIENT-ID").value
       client_secret = secret_client.get_secret("AI-CLIENT-SECRET").value

       return client_id, client_secret
   except Exception as e:
       print(f"Error retrieving credentials from Key Vault: {str(e)}")
       raise

```

Best practices for credential management: - Store secrets in Azure Key Vault - Rotate secrets every 90 days - Implement proper error handling - Monitor access patterns using Azure Monitor

  1. Access Control
  2. Follow least privilege
  3. Regular access reviews
  4. Role-based permissions
  5. Resource-level controls

  6. Environment Security

  7. Secure configuration storage
  8. Environment separation
  9. Audit logging
  10. Monitoring and alerts

Troubleshooting

  1. Common Issues and Solutions ```python from azure.core.exceptions import ClientAuthenticationError

def validate_authentication(): """Validate authentication setup and troubleshoot common issues.""" try: # Initialize client client = get_ai_client()

       # Test authentication by listing available models
       models = client.models.list()
       print("Authentication successful!")
       return True

   except ClientAuthenticationError as auth_error:
       print(f"Authentication failed: {str(auth_error)}")
       print("\nTroubleshooting steps:")
       print("1. Verify environment variables are set correctly")
       print("2. Check if service principal has required permissions")
       print("3. Ensure credentials haven't expired")
       return False

   except Exception as e:
       print(f"Unexpected error: {str(e)}")
       print("\nTroubleshooting steps:")
       print("1. Check network connectivity")
       print("2. Verify Azure subscription status")
       print("3. Review resource group access")
       return False

```

  1. Resolution Steps ```bash # Verify environment variables echo $AZURE_TENANT_ID echo $AZURE_CLIENT_ID echo $AZURE_SUBSCRIPTION_ID

# Check network access ping management.azure.com

# Verify Azure CLI login az account show

# Test resource group access az group show --name $AZURE_RESOURCE_GROUP ```

Interactive Authentication Workshop

For hands-on practice with authentication setup and troubleshooting, try our interactive notebook:

Launch Authentication Workshop

This notebook provides: - Practical authentication setup examples - Environment variable configuration - Real-time authentication testing - Interactive troubleshooting guide - Best practices implementation

Interactive Workshop

For hands-on practice with authentication setup and troubleshooting, try our interactive notebook:

Launch Project Setup Workshop

This notebook provides: - Practical authentication setup examples - Environment variable configuration - Real-time authentication testing - Interactive troubleshooting guide - Best practices implementation

Next: Working with AIProjectClient