Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Analyze and reduce Azure cloud costs by right-sizing resources, reservations, and spending policies
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
cost-optimization/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## Golden Rule67Use **managed identities** and **Azure RBAC** in production. Reserve `DefaultAzureCredential` for **local development only**.89## Authentication by Environment1011| Environment | Recommended Credential | Why |12|---|---|---|13| **Production (Azure-hosted)** | `ManagedIdentityCredential` (system- or user-assigned) | No secrets to manage; auto-rotated by Azure |14| **Production (on-premises)** | `ClientCertificateCredential` or `WorkloadIdentityCredential` | Deterministic; no fallback chain overhead |15| **CI/CD pipelines** | `AzurePipelinesCredential` / `WorkloadIdentityCredential` | Scoped to pipeline identity |16| **Local development** | `DefaultAzureCredential` | Chains CLI, PowerShell, and VS Code credentials for convenience |1718## Why Not `DefaultAzureCredential` in Production?19201. **Unpredictable fallback chain** — walks through multiple credential types, adding latency and making failures harder to diagnose.212. **Broad surface area** — checks environment variables, CLI tokens, and other sources that should not exist in production.223. **Non-deterministic** — which credential actually authenticates depends on the environment, making behavior inconsistent across deployments.234. **Performance** — each failed credential attempt adds network round-trips before falling back to the next.2425## Production Patterns2627### .NET2829```csharp30using Azure.Identity;3132var credential = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT") == "Development"33? new DefaultAzureCredential() // local dev — uses CLI/VS credentials34: new ManagedIdentityCredential(); // production — deterministic, no fallback chain35// For user-assigned identity: new ManagedIdentityCredential("<client-id>")36```3738### TypeScript / JavaScript3940```typescript41import { DefaultAzureCredential, ManagedIdentityCredential } from "@azure/identity";4243const credential = process.env.NODE_ENV === "development"44? new DefaultAzureCredential() // local dev — uses CLI/VS credentials45: new ManagedIdentityCredential(); // production — deterministic, no fallback chain46// For user-assigned identity: new ManagedIdentityCredential("<client-id>")47```4849### Python5051```python52import os53from azure.identity import DefaultAzureCredential, ManagedIdentityCredential5455credential = (56DefaultAzureCredential() # local dev — uses CLI/VS credentials57if os.getenv("AZURE_FUNCTIONS_ENVIRONMENT") == "Development"58else ManagedIdentityCredential() # production — deterministic, no fallback chain59)60# For user-assigned identity: ManagedIdentityCredential(client_id="<client-id>")61```6263### Java6465```java66import com.azure.identity.DefaultAzureCredentialBuilder;67import com.azure.identity.ManagedIdentityCredentialBuilder;6869var credential = "Development".equals(System.getenv("AZURE_FUNCTIONS_ENVIRONMENT"))70? new DefaultAzureCredentialBuilder().build() // local dev — uses CLI/VS credentials71: new ManagedIdentityCredentialBuilder().build(); // production — deterministic, no fallback chain72// For user-assigned identity: new ManagedIdentityCredentialBuilder().clientId("<client-id>").build()73```7475## Local Development Setup7677`DefaultAzureCredential` is ideal for local dev because it automatically picks up credentials from developer tools:78791. **Azure CLI** — `az login`802. **Azure Developer CLI** — `azd auth login`813. **Azure PowerShell** — `Connect-AzAccount`824. **Visual Studio / VS Code** — sign in via Azure extension8384```typescript85import { DefaultAzureCredential } from "@azure/identity";8687// Local development only — uses CLI/PowerShell/VS Code credentials88const credential = new DefaultAzureCredential();89```9091## Environment-Aware Pattern9293Detect the runtime environment and select the appropriate credential. The key principle: use `DefaultAzureCredential` only when running locally, and a specific credential in production.9495> **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`).9697```typescript98import { DefaultAzureCredential, ManagedIdentityCredential } from "@azure/identity";99100function getCredential() {101if (process.env.NODE_ENV === "development") {102return new DefaultAzureCredential(); // picks up az login / VS Code creds103}104return process.env.AZURE_CLIENT_ID105? new ManagedIdentityCredential(process.env.AZURE_CLIENT_ID) // user-assigned106: new ManagedIdentityCredential(); // system-assigned107}108```109110## Security Checklist111112- [ ] Use managed identity for all Azure-hosted apps113- [ ] Never hardcode credentials, connection strings, or keys114- [ ] Apply least-privilege RBAC roles at the narrowest scope115- [ ] Use `ManagedIdentityCredential` (not `DefaultAzureCredential`) in production116- [ ] Store any required secrets in Azure Key Vault117- [ ] Rotate secrets and certificates on a schedule118- [ ] Enable Microsoft Defender for Cloud on production resources119120## Further Reading121122- [Passwordless connections overview](https://learn.microsoft.com/azure/developer/intro/passwordless-overview)123- [Managed identities overview](https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview)124- [Azure RBAC overview](https://learn.microsoft.com/azure/role-based-access-control/overview)125- [.NET authentication guide](https://learn.microsoft.com/dotnet/azure/sdk/authentication/)126- [Python identity library](https://learn.microsoft.com/python/api/overview/azure/identity-readme)127- [JavaScript identity library](https://learn.microsoft.com/javascript/api/overview/azure/identity-readme)128- [Java identity library](https://learn.microsoft.com/java/api/overview/azure/identity-readme)129