Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build and deploy AI applications on Azure AI Foundry using Microsoft's model catalog and AI services
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/auth-best-practices.md
1# Azure Authentication Best Practices23> Source: [Microsoft — Passwordless connections for Azure services](https://learn.microsoft.com/azure/developer/intro/passwordless-overview) and [Azure Identity client libraries](https://learn.microsoft.com/dotnet/azure/sdk/authentication/).45**Table of Contents:** [Golden Rule](#golden-rule) · [Authentication by Environment](#authentication-by-environment) · [Why Not DefaultAzureCredential in Production?](#why-not-defaultazurecredential-in-production) · [Production Patterns](#production-patterns) · [Local Development Setup](#local-development-setup) · [Environment-Aware Pattern](#environment-aware-pattern) · [Security Checklist](#security-checklist) · [Further Reading](#further-reading)67## Golden Rule89Use **managed identities** and **Azure RBAC** in production. Reserve `DefaultAzureCredential` for **local development only**.1011## Authentication by Environment1213| Environment | Recommended Credential | Why |14|---|---|---|15| **Production (Azure-hosted)** | `ManagedIdentityCredential` (system- or user-assigned) | No secrets to manage; auto-rotated by Azure |16| **Production (on-premises)** | `ClientCertificateCredential` or `WorkloadIdentityCredential` | Deterministic; no fallback chain overhead |17| **CI/CD pipelines** | `AzurePipelinesCredential` / `WorkloadIdentityCredential` | Scoped to pipeline identity |18| **Local development** | `DefaultAzureCredential` | Chains CLI, PowerShell, and VS Code credentials for convenience |1920## Why Not `DefaultAzureCredential` in Production?21221. **Unpredictable fallback chain** — walks through multiple credential types, adding latency and making failures harder to diagnose.232. **Broad surface area** — checks environment variables, CLI tokens, and other sources that should not exist in production.243. **Non-deterministic** — which credential actually authenticates depends on the environment, making behavior inconsistent across deployments.254. **Performance** — each failed credential attempt adds network round-trips before falling back to the next.2627## Production Patterns2829### .NET3031```csharp32using Azure.Identity;3334var credential = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT") == "Development"35? new DefaultAzureCredential() // local dev — uses CLI/VS credentials36: new ManagedIdentityCredential(); // production — deterministic, no fallback chain37// For user-assigned identity: new ManagedIdentityCredential("<client-id>")38```3940### TypeScript / JavaScript4142```typescript43import { DefaultAzureCredential, ManagedIdentityCredential } from "@azure/identity";4445const credential = process.env.NODE_ENV === "development"46? new DefaultAzureCredential() // local dev — uses CLI/VS credentials47: new ManagedIdentityCredential(); // production — deterministic, no fallback chain48// For user-assigned identity: new ManagedIdentityCredential("<client-id>")49```5051### Python5253```python54import os55from azure.identity import DefaultAzureCredential, ManagedIdentityCredential5657credential = (58DefaultAzureCredential() # local dev — uses CLI/VS credentials59if os.getenv("AZURE_FUNCTIONS_ENVIRONMENT") == "Development"60else ManagedIdentityCredential() # production — deterministic, no fallback chain61)62# For user-assigned identity: ManagedIdentityCredential(client_id="<client-id>")63```6465### Java6667```java68import com.azure.identity.DefaultAzureCredentialBuilder;69import com.azure.identity.ManagedIdentityCredentialBuilder;7071var credential = "Development".equals(System.getenv("AZURE_FUNCTIONS_ENVIRONMENT"))72? new DefaultAzureCredentialBuilder().build() // local dev — uses CLI/VS credentials73: new ManagedIdentityCredentialBuilder().build(); // production — deterministic, no fallback chain74// For user-assigned identity: new ManagedIdentityCredentialBuilder().clientId("<client-id>").build()75```7677## Local Development Setup7879`DefaultAzureCredential` is ideal for local dev because it automatically picks up credentials from developer tools:80811. **Azure CLI** — `az login`822. **Azure Developer CLI** — `azd auth login`833. **Azure PowerShell** — `Connect-AzAccount`844. **Visual Studio / VS Code** — sign in via Azure extension8586```typescript87import { DefaultAzureCredential } from "@azure/identity";8889// Local development only — uses CLI/PowerShell/VS Code credentials90const credential = new DefaultAzureCredential();91```9293## Environment-Aware Pattern9495Detect the runtime environment and select the appropriate credential. The key principle: use `DefaultAzureCredential` only when running locally, and a specific credential in production.9697> **Tip:** Azure Functions sets `AZURE_FUNCTIONS_ENVIRONMENT` to `"Development"` when running locally. For App Service or containers, use any environment variable you control (e.g. `NODE_ENV`, `ASPNETCORE_ENVIRONMENT`).9899```typescript100import { DefaultAzureCredential, ManagedIdentityCredential } from "@azure/identity";101102function getCredential() {103if (process.env.NODE_ENV === "development") {104return new DefaultAzureCredential(); // picks up az login / VS Code creds105}106return process.env.AZURE_CLIENT_ID107? new ManagedIdentityCredential(process.env.AZURE_CLIENT_ID) // user-assigned108: new ManagedIdentityCredential(); // system-assigned109}110```111112## Security Checklist113114- [ ] Use managed identity for all Azure-hosted apps115- [ ] Never hardcode credentials, connection strings, or keys116- [ ] Apply least-privilege RBAC roles at the narrowest scope117- [ ] Use `ManagedIdentityCredential` (not `DefaultAzureCredential`) in production118- [ ] Store any required secrets in Azure Key Vault119- [ ] Rotate secrets and certificates on a schedule120- [ ] Enable Microsoft Defender for Cloud on production resources121122## Further Reading123124- [Passwordless connections overview](https://learn.microsoft.com/azure/developer/intro/passwordless-overview)125- [Managed identities overview](https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview)126- [Azure RBAC overview](https://learn.microsoft.com/azure/role-based-access-control/overview)127- [.NET authentication guide](https://learn.microsoft.com/dotnet/azure/sdk/authentication/)128- [Python identity library](https://learn.microsoft.com/python/api/overview/azure/identity-readme)129- [JavaScript identity library](https://learn.microsoft.com/javascript/api/overview/azure/identity-readme)130- [Java identity library](https://learn.microsoft.com/java/api/overview/azure/identity-readme)131