-->

02/04/2026

Authenticating Azure Foundry OpenAI Using Managed Identity

In earlier post we have tested 30+ features of various Azure Foundry Open AI services.

Post: https://pratapreddypilaka.blogspot.com/2026/04/azure-ai-services-complete-guide-to.html

Most of the code have saved all the Open AI service keys in an environment file following Python best practice. But this is done only for practice purpose, just to get familiar with Open AI services and how to access those services using Python code.

Saving the Access keys in code is the worst possible security blunder you can do. Access keys will completely bypass all the RBAC controls and give complete access to anyone.

What are the ways to authenticate Azure Open AI services without exposing the keys?

  • System Assigned Managed Identity — Auto-created identity tied to an Azure resource (VM, App Service, Functions)
  • User Assigned Managed Identity — Standalone identity you create and attach to one or more Azure resources
  • Service Principal + Client Secret — App registration with a secret, works anywhere including on-prem
  • Service Principal + Certificate — Same as above but uses a certificate instead of secret, more secure
  • Azure CLI Credential — Uses the logged-in az login identity, ideal for local dev/testing
  • Interactive Browser Credential — Pops a browser login window, good for desktop tools
  • Device Code Credential — Prints a code to enter at a URL, useful for headless servers
  • DefaultAzureCredential — Tries multiple methods automatically in order, recommended for most cases
  • Workload Identity — Federated keyless auth for pods running in Azure Kubernetes Service (AKS)
  • Federated Identity Credential — Allows external IdPs like GitHub Actions or GitLab to authenticate without any secrets

Managed Identity is the preferred method for Azure workloads to access Open AI services.

In this article we will have a look at how we can authenticate using Managed Identity.

For this we need a VM up on which we will enable system managed Identity.

I created a Linux VM, and enabled the managed identity.

Now, go to Foundry, go to Access control, and Add a new role assignment for the managed identity we created earlier with role being "Cognitive Services OpenAI User".

Now create the rules for NSG to open the communication between OpenAI services. This is necessary if you are using a private endpoint for the OpenAI instance.

Login to your virtual machine.

Now in order to test the connectivity, I am installing Python and Azure-Identity.

# Python & pip
sudo apt update && sudo apt install python3-pip -y

# Required packages
pip3 install openai azure-identity

Once all the dependencies are installed, we will create a file with the below code.

from azure.identity import ManagedIdentityCredential
from openai import AzureOpenAI
import os

# ── Config ──────────────────────────────────────────────
AZURE_OPENAI_ENDPOINT = "https://prata-mhl58p7n-eastus2.cognitiveservices.azure.com/"
DEPLOYMENT_NAME       = "gpt-5-chat"   # e.g. gpt-4o
API_VERSION           = "2024-02-01"
# ────────────────────────────────────────────────────────

# 1. Obtain a token via Managed Identity (no keys!)
credential = ManagedIdentityCredential()
token       = credential.get_token("https://cognitiveservices.azure.com/.default")

# 2. Build AzureOpenAI client using the bearer token
client = AzureOpenAI(
    azure_endpoint = AZURE_OPENAI_ENDPOINT,
    api_version    = API_VERSION,
    azure_ad_token = token.token,       # <-- token-based, not key-based
)

# 3. Call the model
response = client.chat.completions.create(
    model    = DEPLOYMENT_NAME,
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user",   "content": "Hello! Tell me more about Managed identity authentication without using access keys for Azure Open AI"}
    ]
)

print("✅ Response:", response.choices[0].message.content)

Now run the python file python3 chat.py

You will get the response from your gpt-5-chat model, without using access keys.

Azure AI Services - A Complete Guide to Building Intelligent Applications with Azure AI Foundry

A while ago i started exploring Azure AI Foundry and ended up going down a rabbit hole of 30+ implementations covering everything from GPT-5 chat to live speech transcription. In this post i will walk you through all the major Azure AI services, what they do, how to implement them, and when to use them — so you don't have to figure it all out the hard way like i did.

You can download git repo and start embedding your Azure OpenAI Service keys in.env file and start executing them as we go along.

Our objective is to understand the complete Azure AI Services ecosystem and how you can combine them to build enterprise-grade intelligent applications.


Azure OpenAI - GPT-5 Chat, Vision and Code

This is where most people start, and for good reason. Azure OpenAI gives you access to GPT-5 with enterprise-grade security, regional deployment and SLAs — unlike calling OpenAI directly.

The basic setup is straightforward. You initialize an AzureOpenAI client with your endpoint and API key, define a system role (something like "you are a helpful travel assistant"), pass in user messages and configure temperature and top_p for response behavior. That's it, you are doing conversational AI.

from openai import AzureOpenAI
from azure.core.credentials import AzureKeyCredential
from dotenv import load_dotenv
import os

load_dotenv()
client = AzureOpenAI(
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key=os.getenv("AZURE_OPENAI_KEY"),
    api_version="2024-12-01-preview"
)

What makes it more interesting is Vision. You can encode an image to base64, pass it as image_url in the message content, and GPT-5 will analyze and explain it — diagrams, screenshots, anything. I used this for code explanation too. Point it at a source file with a "you are a teacher" system prompt and let it stream the explanation back. Really useful for documentation generation and code reviews.

Chat Output:


Image Reading Output: