alpha

Lays out authentication and secret-handling patterns for the Dataverse Python SDK using Azure Identity credentials, from local development to production.
applyTo: '**'
Based on official Microsoft Azure SDK authentication documentation and Dataverse SDK best practices.
The Dataverse SDK for Python uses Azure Identity credentials for token-based authentication. This approach follows the principle of least privilege and works across local development, cloud deployment, and on-premises environments.
Advantages over connection strings:
Use for: Developer workstations during local development.
from azure.identity import InteractiveBrowserCredential
from PowerPlatform.Dataverse.client import DataverseClient
# Opens browser for authentication
credential = InteractiveBrowserCredential()
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
# First use prompts for sign-in; subsequent calls use cached token
records = client.get("account")
When to use:
Use for: Apps that run in multiple environments (dev → test → production).
from azure.identity import DefaultAzureCredential
from PowerPlatform.Dataverse.client import DataverseClient
# Attempts credentials in this order:
# 1. Environment variables (app service principal)
# 2. Azure CLI credentials (local development)
# 3. Azure PowerShell credentials (local development)
# 4. Managed identity (when running in Azure)
credential = DefaultAzureCredential()
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
records = client.get("account")
Advantages:
Credential chain:
AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET)az login)Connect-AzAccount)Use for: Unattended authentication (scheduled jobs, scripts, on-premises services).
from azure.identity import ClientSecretCredential
from PowerPlatform.Dataverse.client import DataverseClient
import os
credential = ClientSecretCredential(
tenant_id=os.environ["AZURE_TENANT_ID"],
client_id=os.environ["AZURE_CLIENT_ID"],
client_secret=os.environ["AZURE_CLIENT_SECRET"]
)
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
records = client.get("account")
Setup steps:
Security concerns:
Use for: Apps hosted in Azure (App Service, Azure Functions, AKS, VMs).
from azure.identity import ManagedIdentityCredential
from PowerPlatform.Dataverse.client import DataverseClient
# No secrets needed - Azure manages identity
credential = ManagedIdentityCredential()
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
records = client.get("account")
Benefits:
Setup:
# .env file (git-ignored)
DATAVERSE_URL=https://myorg-dev.crm.dynamics.com
# Python code
import os
from azure.identity import DefaultAzureCredential
from PowerPlatform.Dataverse.client import DataverseClient
# Uses your Azure CLI credentials
credential = DefaultAzureCredential()
client = DataverseClient(
base_url=os.environ["DATAVERSE_URL"],
credential=credential
)
Setup: az login with your developer account
from azure.identity import ManagedIdentityCredential
from PowerPlatform.Dataverse.client import DataverseClient
# Automatically uses managed identity
credential = ManagedIdentityCredential()
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
Setup: Enable managed identity in App Service, grant permissions in Dataverse
import os
from azure.identity import ClientSecretCredential
from PowerPlatform.Dataverse.client import DataverseClient
credential = ClientSecretCredential(
tenant_id=os.environ["AZURE_TENANT_ID"],
client_id=os.environ["AZURE_CLIENT_ID"],
client_secret=os.environ["AZURE_CLIENT_SECRET"]
)
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
Setup: Create service principal, store credentials securely, grant Dataverse permissions
from PowerPlatform.Dataverse.core.config import DataverseConfig
from azure.identity import DefaultAzureCredential
from PowerPlatform.Dataverse.client import DataverseClient
cfg = DataverseConfig()
cfg.logging_enable = True # Enable detailed logging
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=DefaultAzureCredential(),
config=cfg
)
from PowerPlatform.Dataverse.core.config import DataverseConfig
cfg = DataverseConfig()
# Timeout settings
cfg.http_timeout = 30 # Request timeout in seconds
# Retry configuration
cfg.http_retries = 3 # Number of retry attempts
cfg.http_backoff = 1 # Initial backoff in seconds
# Connection reuse
cfg.connection_timeout = 5 # Connection timeout
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential,
config=cfg
)
# ❌ BAD - Don't do this!
credential = ClientSecretCredential(
tenant_id="your-tenant-id",
client_id="your-client-id",
client_secret="your-secret-key" # EXPOSED!
)
# ✅ GOOD - Use environment variables
import os
credential = ClientSecretCredential(
tenant_id=os.environ["AZURE_TENANT_ID"],
client_id=os.environ["AZURE_CLIENT_ID"],
client_secret=os.environ["AZURE_CLIENT_SECRET"]
)
Development:
# .env file (git-ignored)
AZURE_TENANT_ID=your-tenant-id
AZURE_CLIENT_ID=your-client-id
AZURE_CLIENT_SECRET=your-secret-key
Production:
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
# Retrieve secrets from Azure Key Vault
credential = DefaultAzureCredential()
client = SecretClient(
vault_url="https://mykeyvault.vault.azure.net",
credential=credential
)
secret = client.get_secret("dataverse-client-secret")
# Grant minimal permissions:
# - Only read if app only reads
# - Only specific tables if possible
# - Time-limit credentials (auto-rotation)
# - Use managed identity instead of shared secrets
import logging
logger = logging.getLogger("dataverse_auth")
try:
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
logger.info("Successfully authenticated to Dataverse")
except Exception as e:
logger.error(f"Authentication failed: {e}")
raise
from azure.core.exceptions import ClientAuthenticationError
import time
def create_with_auth_retry(client, table_name, payload, max_retries=2):
"""Create record, retrying if token expired."""
for attempt in range(max_retries):
try:
return client.create(table_name, payload)
except ClientAuthenticationError:
if attempt < max_retries - 1:
logger.warning("Token expired, retrying...")
time.sleep(1)
else:
raise
from azure.identity import DefaultAzureCredential
from PowerPlatform.Dataverse.client import DataverseClient
def get_client_for_tenant(tenant_id: str) -> DataverseClient:
"""Get DataverseClient for specific tenant."""
credential = DefaultAzureCredential()
# Dataverse URL contains tenant-specific org
base_url = f"https://{get_org_for_tenant(tenant_id)}.crm.dynamics.com"
return DataverseClient(
base_url=base_url,
credential=credential
)
def get_org_for_tenant(tenant_id: str) -> str:
"""Map tenant to Dataverse organization."""
# Implementation depends on your multi-tenant strategy
# Could be database lookup, configuration, etc.
pass
try:
client.get("account")
except DataverseError as e:
if e.status_code == 403:
print("User/app lacks Dataverse permissions")
print("Ensure Dataverse security role is assigned")
# Check credential source
from azure.identity import DefaultAzureCredential
try:
cred = DefaultAzureCredential(exclude_cli_credential=False,
exclude_powershell_credential=False)
# Force re-authentication
import subprocess
subprocess.run(["az", "login"])
except Exception as e:
print(f"Authentication failed: {e}")
# Verify tenant ID
import json
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
token = credential.get_token("https://dataverse.dynamics.com/.default")
# Decode token to verify tenant
import base64
payload = base64.b64decode(token.token.split('.')[1] + '==')
claims = json.loads(payload)
print(f"Token tenant: {claims.get('tid')}")
Azure Identity handles token refresh automatically:
# Tokens are cached and refreshed automatically
credential = DefaultAzureCredential()
# First call acquires token
client.get("account")
# Subsequent calls reuse cached token
client.get("contact")
# If token expires, SDK automatically refreshes
class DataverseSession:
"""Manages DataverseClient lifecycle."""
def __init__(self, base_url: str):
from azure.identity import DefaultAzureCredential
self.client = DataverseClient(
base_url=base_url,
credential=DefaultAzureCredential()
)
def __enter__(self):
return self.client
def __exit__(self, exc_type, exc_val, exc_tb):
# Cleanup if needed
pass
# Usage
with DataverseSession("https://myorg.crm.dynamics.com") as client:
records = client.get("account")
User's Dataverse security role determines accessible records:
from azure.identity import InteractiveBrowserCredential
from PowerPlatform.Dataverse.client import DataverseClient
# Each user gets client with their credentials
def get_user_client(user_username: str) -> DataverseClient:
# User must already be authenticated
credential = InteractiveBrowserCredential()
client = DataverseClient(
base_url="https://myorg.crm.dynamics.com",
credential=credential
)
# User only sees records they have access to
return client
Assign minimal required roles:
© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com


Quiet Kyoto autumn travel vlog, 30 seconds
Generates a slow, natural 30-second autumn travel vlog in Kyoto of the woman from a reference image, with short casual dialogue, handheld camera and film grain, avoiding an ad look.
video-prompt, vlog, reference-image, character-consistency, handheld, film-grain, home-video, travel, japan, multi-shot, lip-sync, natural-light, guardrails, seedance, for-content-creators, timed-beats, long-form, casual, candid, autumn

Ice heroine fights hunters in Seoul parking garage
Plans a 30-second supernatural action film where an ice-powered heroine defends a puppy from hunters in a Seoul parking garage, with identity locked to two reference images.
video-prompt, kling, timed-beats, multi-shot, action, martial-arts, visual-effects, character-consistency, reference-image, korea, cityscape, night-scene, slow-motion, tracking-shot, cinematic, creatures, film-score, negative-prompt, dramatic-lighting, neon, advanced, lip-sync, woman

Floating pan-fried gyoza with ponzu splash
Generates a macro food photograph of pan-fried gyoza floating in zero gravity with a ponzu splash, sesame droplets and green onion confetti under warm side light.
image-prompt, food-photography, photorealistic, cinematic, macro, nano-banana, dramatic-lighting, vertical-format, still-life, cooking, japan, golden-hour, for-photographers, restaurants, bokeh, advanced
Loading reviews…
Loading comments…