---
title: Call backends with managed identities
summary: Let the gateway reach Microsoft Foundry with a managed identity instead of stored keys. Check the system-assigned identity, then create a user-assigned identity you can share across gateways, grant it Foundry User, attach it, and point a model provider at it from the portal, the Azure CLI, Bicep, or Terraform.
category: security
level: Intermediate
duration: 20
tracks: [portal, cli, iac, terraform]
order: 12
illustration: diagrams/identity-boundaries.svg
tags: [managed identity, user-assigned identity, Foundry User, RBAC, keyless, model provider, Terraform, "403"]
objectives:
  - Explain how the gateway authenticates to backends, separately from how callers authenticate to the gateway
  - Check the gateway's system-assigned identity and the assets that use it
  - Create a user-assigned identity, attach it to the gateway, and grant it Foundry User on a Foundry account
  - Point a Foundry model provider at the user-assigned identity and prove that calls still succeed
  - Decide when a backend needs a stored API key instead
prereqs:
  - 02-first-model-call
---

# Call backends with managed identities

Your platform team runs one gateway per region, and every gateway calls the same Foundry account. Today each
gateway uses its own system-assigned identity, so every new gateway needs a fresh **Foundry User** grant, and
recreating a gateway leaves a stale role assignment behind. The team wants one identity that it owns, grants
once, and attaches to every gateway, with no backend key stored anywhere. In this lab you check the gateway's
system-assigned identity, create a user-assigned identity, give it access to Foundry, and switch a model
provider to it without changing a single client.

![Callers authenticate inbound to the gateway, and the gateway authenticates outbound to each backend](../assets/diagrams/identity-boundaries.svg "Callers authenticate to the gateway with an API key or a Microsoft Entra token. The gateway then calls each backend with its own managed identity, which this lab configures.")

## Before you begin

<!-- setup: tools sign-in extension clone -->

> [!TIP]
> On the **Bicep + azd** and **Terraform** tracks, `azd up` or `terraform apply` creates its own gateway, model, and
> key, so you can skip this section. You need only the one-time setup above, and permission to create role
> assignments, for example **Owner** on the subscription. To check the system-assigned identity first, run the track
> in [Create a user-assigned identity and give it access](#create-a-user-assigned-identity-and-give-it-access) up to
> **Read the outputs and the key**, which sets the variables, then come back.

- Complete [Publish a Foundry model and make your first call](../02-first-model-call/README.md). You need a
  gateway with a Foundry model provider that uses a managed identity, a model alias, and a runtime API key.
- Every track uses the Azure CLI at least once: the portal can't change the identity of a model provider that already
  exists.
- Permissions:
  - Create a managed identity in the resource group, for example **Contributor** or
    **Managed Identity Contributor**.
  - Attach the identity to the gateway: **Contributor** on the gateway and **Managed Identity Operator** on the
    identity. **Contributor** on a resource group that holds both covers this.
  - Create role assignments on the Foundry account, for example **Owner**, **User Access Administrator**, or
    **Role Based Access Control Administrator**.

Managed identities are free. Nothing in this lab adds charges beyond the model calls you make.

Set these variables once. Every track and the **Verify** section reuse them.

```bash title="Bash"
# Set your gateway, its Foundry provider and Foundry account, and the name of the user-assigned identity to create
export RG="<resource-group>"
export GATEWAY="<gateway>"
export PROVIDER="<provider-name>"                 # the Foundry provider from lab 02, for example foundry-models
export FOUNDRY_RG="<foundry-resource-group>"      # resource group of the Foundry account
export FOUNDRY_ACCOUNT="<foundry-account-name>"   # the account name, not a project or deployment name
export IDENTITY="id-aigateway-backends"           # the user-assigned identity to create
echo "📝 Gateway $GATEWAY in $RG, with provider $PROVIDER and Foundry account $FOUNDRY_ACCOUNT"
echo "👤 Identity to create: $IDENTITY"

# Read the Foundry account's resource ID and the gateway's runtime URL, and set the alias that you call
export FOUNDRY_ID="$(az cognitiveservices account show -g "$FOUNDRY_RG" -n "$FOUNDRY_ACCOUNT" --query id -o tsv)"
export AI_GATEWAY_URL="https://$(az aigateway show -g "$RG" -n "$GATEWAY" --query properties.frontend.defaultHostname -o tsv)"
export AI_GATEWAY_MODEL="<model-name>"            # the alias you called in lab 02
echo "🆔 Foundry account: $FOUNDRY_ID"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"
echo "🔖 Model alias: $AI_GATEWAY_MODEL"

# Paste the runtime key from lab 02 without showing it, and don't commit it or write it to a file
printf "AI Gateway API key: " && read -rs AI_GATEWAY_API_KEY && export AI_GATEWAY_API_KEY && echo
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"
```

```powershell title="PowerShell"
# Set your gateway, its Foundry provider and Foundry account, and the name of the user-assigned identity to create
$RG              = "<resource-group>"
$GATEWAY         = "<gateway>"
$PROVIDER        = "<provider-name>"              # the Foundry provider from lab 02, for example foundry-models
$FOUNDRY_RG      = "<foundry-resource-group>"     # resource group of the Foundry account
$FOUNDRY_ACCOUNT = "<foundry-account-name>"       # the account name, not a project or deployment name
$IDENTITY        = "id-aigateway-backends"        # the user-assigned identity to create
Write-Host "📝 Gateway $GATEWAY in $RG, with provider $PROVIDER and Foundry account $FOUNDRY_ACCOUNT"
Write-Host "👤 Identity to create: $IDENTITY"

# Read the Foundry account's resource ID and the gateway's runtime URL, and set the alias that you call
$FOUNDRY_ID = az cognitiveservices account show -g $FOUNDRY_RG -n $FOUNDRY_ACCOUNT --query id -o tsv
$env:AI_GATEWAY_URL = "https://$(az aigateway show -g $RG -n $GATEWAY --query properties.frontend.defaultHostname -o tsv)"
$env:AI_GATEWAY_MODEL = "<model-name>"            # the alias you called in lab 02
Write-Host "🆔 Foundry account: $FOUNDRY_ID"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"
Write-Host "🔖 Model alias: $env:AI_GATEWAY_MODEL"

# Paste the runtime key from lab 02 without showing it, and don't commit it or write it to a file
$env:AI_GATEWAY_API_KEY = Read-Host "AI Gateway API key" -MaskInput
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"
```

Don't remember the provider name? List the Foundry providers:
`az aigateway model-provider list -g "$RG" --gateway-name "$GATEWAY" --query "[?properties.modelProviderType=='Foundry'].name" -o tsv`.

## How it works

Every call crosses two boundaries. **Inbound**, the caller proves who it is to the gateway with a runtime API key
([lab 10](../10-api-keys/README.md)) or a Microsoft Entra token ([lab 11](../11-entra-rbac/README.md)).
**Outbound**, the gateway proves who *it* is to the backend. The caller never sends a backend credential: you
configure it once on the asset, such as a model provider or an MCP server backend.

For Azure backends, use a managed identity. The gateway requests a Microsoft Entra token for the backend's
audience, `https://cognitiveservices.azure.com/` for Foundry, and Azure RBAC on the backend decides what the
identity can do. For Foundry and Azure OpenAI, grant **Foundry User** on the account. Keep backend API keys for
backends that can't accept an Entra token.

| | System-assigned identity | User-assigned identity | Stored backend key |
|---|---|---|---|
| Created by | Azure, with the gateway. The portal enables it by default. | You, as its own Azure resource | The backend's owner |
| Lifecycle | Tied to the gateway. Deleting the gateway deletes it. | Independent. Attach it to many gateways and keep it when a gateway is recreated. | You store it in the gateway and rotate it |
| Best for | One gateway calling Azure backends | Sharing one identity and its role assignments across gateways | Third-party providers and tools without Entra support |
| Foundry provider setting | `authentication.managedIdentity` without `clientId` | `authentication.managedIdentity.clientId` set to the identity's client ID | `authentication.headers`, for example an `api-key` header |

Attaching an identity to the gateway doesn't change which identity an asset uses. Each asset names its identity,
so you can move assets one at a time.

The table uses the field names of API version `2026-11-01`, where `authentication` sits at the top level of the
provider's properties, next to `foundry`. The `az aigateway` output shows the same shape. Only its `--foundry`
argument still takes `authentication` as one of the Foundry settings, and the CLI writes it to the top level.

## Check the system-assigned identity

Lab 02's provider calls Foundry with the gateway's system-assigned identity. See it before you change anything.

<!-- steps -->

### Look at the identity in the portal

1. Open the [AI Gateway portal](https://ai.gateway.azure.com) and select your gateway.
2. Select **Managed identities**. On **Configure identities**, the **System-assigned identity** card shows
   **Enabled** with a **Principal ID** and a **Tenant ID**.
3. Select **View access**. **Backend access** lists the assets that authenticate with a managed identity. Your
   Foundry provider appears with **System-assigned** as its identity and **Foundry User** as its role.

![The Managed identities page with the system-assigned identity enabled and no user-assigned identities](../assets/screens/managed-identities.png "Configure identities shows the system-assigned identity's principal and tenant IDs, and the user-assigned identities attached to the gateway.")

### Check it with the CLI

```bash title="Bash"
# Show the gateway's identities: the type, and the system-assigned identity's principal and tenant IDs
echo "👤 Identities of $GATEWAY:"
az aigateway identity show -g "$RG" -n "$GATEWAY" -o json
```

```powershell title="PowerShell"
# Show the gateway's identities: the type, and the system-assigned identity's principal and tenant IDs
Write-Host "👤 Identities of ${GATEWAY}:"
az aigateway identity show -g $RG -n $GATEWAY -o json
```

The output shows `type`, which includes `SystemAssigned`, and the system-assigned `principalId` and `tenantId`.
If the command reports that no identity is assigned to the gateway, enable the system-assigned identity and grant
the new principal **Foundry User** on the Foundry account. `identity assign` returns when the gateway is ready
again:

```bash title="Bash"
# Turn on the gateway's system-assigned identity, and wait until the gateway is ready again
az aigateway identity assign -g "$RG" -n "$GATEWAY" --system-assigned true -o none &&
  echo "👤 The system-assigned identity of $GATEWAY is on"

# Read the principal ID of the system-assigned identity
PRINCIPAL_ID="$(az aigateway show -g "$RG" -n "$GATEWAY" --query identity.principalId -o tsv)"
echo "👤 Gateway identity: $PRINCIPAL_ID"

# Give the identity the Foundry User role on the Foundry account, so that the gateway can call its models
az role assignment create --assignee-object-id "$PRINCIPAL_ID" --assignee-principal-type ServicePrincipal \
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "$FOUNDRY_ID" -o none &&
  echo "🔐 The gateway's identity has the Foundry User role on the Foundry account"
```

```powershell title="PowerShell"
# Turn on the gateway's system-assigned identity, and wait until the gateway is ready again
az aigateway identity assign -g $RG -n $GATEWAY --system-assigned true -o none
if ($?) { Write-Host "👤 The system-assigned identity of $GATEWAY is on" }

# Read the principal ID of the system-assigned identity
$PRINCIPAL_ID = az aigateway show -g $RG -n $GATEWAY --query identity.principalId -o tsv
Write-Host "👤 Gateway identity: $PRINCIPAL_ID"

# Give the identity the Foundry User role on the Foundry account, so that the gateway can call its models
az role assignment create --assignee-object-id $PRINCIPAL_ID --assignee-principal-type ServicePrincipal `
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope $FOUNDRY_ID -o none
if ($?) { Write-Host "🔐 The gateway's identity has the Foundry User role on the Foundry account" }
```

<!-- /steps -->

> [!WARNING]
> Don't turn off the system-assigned identity while assets use it. Disabling it recycles the service principal:
> every backend configured with it fails until you enable it again, and a re-enabled identity gets a new
> principal ID without the old role assignments.

## Create a user-assigned identity and give it access

Pick one track. Each track creates the identity, attaches it to the gateway, and grants it **Foundry User** on
the Foundry account.

<!-- track: portal | Portal -->

### Create and attach the identity

1. In **Managed identities** › **Configure identities**, under **User-assigned identities**, select
   **Add identity**.
2. Select **Create new** and, in **Name**, replace the default, the gateway's name, with the name from `$IDENTITY`,
   for example `id-aigateway-backends`. The portal creates the identity in the gateway's subscription, resource
   group, and region, which it shows below the name.
3. Select **Add identity**. The new identity appears in the table with its **Principal** and **Client ID**.

To reuse an identity that already exists, select **Use existing** instead, then pick its **Subscription** and
**User-assigned managed identity**.

### Read the identity's IDs

You need the principal ID to grant access and the client ID to point the provider at the identity.

```bash title="Bash"
# Read the identity's IDs: the resource ID to attach it, the principal ID to grant it access, and the client ID
# to point the provider at it
export IDENTITY_ID="$(az identity show -g "$RG" -n "$IDENTITY" --query id -o tsv)"
export IDENTITY_CLIENT_ID="$(az identity show -g "$RG" -n "$IDENTITY" --query clientId -o tsv)"
export IDENTITY_PRINCIPAL_ID="$(az identity show -g "$RG" -n "$IDENTITY" --query principalId -o tsv)"
echo "🆔 Identity: $IDENTITY_ID"
echo "👤 Client ID: $IDENTITY_CLIENT_ID"
echo "👤 Principal ID: $IDENTITY_PRINCIPAL_ID"
```

```powershell title="PowerShell"
# Read the identity's IDs: the resource ID to attach it, the principal ID to grant it access, and the client ID
# to point the provider at it
$IDENTITY_ID           = az identity show -g $RG -n $IDENTITY --query id -o tsv
$IDENTITY_CLIENT_ID    = az identity show -g $RG -n $IDENTITY --query clientId -o tsv
$IDENTITY_PRINCIPAL_ID = az identity show -g $RG -n $IDENTITY --query principalId -o tsv
Write-Host "🆔 Identity: $IDENTITY_ID"
Write-Host "👤 Client ID: $IDENTITY_CLIENT_ID"
Write-Host "👤 Principal ID: $IDENTITY_PRINCIPAL_ID"
```

### Grant Foundry User on the Foundry account

Attaching an identity doesn't grant it any access. In the [Azure portal](https://portal.azure.com), open the
Foundry account and select **Access control (IAM)** › **Add** › **Add role assignment**. Select **Foundry User**,
assign access to **Managed identity**, select your identity as the member, then select **Review + assign**.

Or grant the role from the command line:

```bash title="Bash"
# Give your identity the Foundry User role on the Foundry account, so that it can call the account's models
az role assignment create --assignee-object-id "$IDENTITY_PRINCIPAL_ID" --assignee-principal-type ServicePrincipal \
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "$FOUNDRY_ID" -o none &&
  echo "🔐 Identity $IDENTITY has the Foundry User role on the Foundry account"
```

```powershell title="PowerShell"
# Give your identity the Foundry User role on the Foundry account, so that it can call the account's models
az role assignment create --assignee-object-id $IDENTITY_PRINCIPAL_ID --assignee-principal-type ServicePrincipal `
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope $FOUNDRY_ID -o none
if ($?) { Write-Host "🔐 Identity $IDENTITY has the Foundry User role on the Foundry account" }
```

> **Checkpoint**
> **User-assigned identities** lists your identity, and the Foundry account's **Access control (IAM)** ›
> **Role assignments** shows it with **Foundry User**.

<!-- track: cli | Azure CLI -->

### Create the identity

```bash title="Bash"
# Create the user-assigned identity, in the resource group's region
az identity create -g "$RG" -n "$IDENTITY" -o none &&
  echo "👤 Identity $IDENTITY is ready"

# Read the identity's IDs: the resource ID to attach it, the principal ID to grant it access, and the client ID
# to point the provider at it
export IDENTITY_ID="$(az identity show -g "$RG" -n "$IDENTITY" --query id -o tsv)"
export IDENTITY_CLIENT_ID="$(az identity show -g "$RG" -n "$IDENTITY" --query clientId -o tsv)"
export IDENTITY_PRINCIPAL_ID="$(az identity show -g "$RG" -n "$IDENTITY" --query principalId -o tsv)"
echo "🆔 Identity: $IDENTITY_ID"
echo "👤 Client ID: $IDENTITY_CLIENT_ID"
echo "👤 Principal ID: $IDENTITY_PRINCIPAL_ID"
```

```powershell title="PowerShell"
# Create the user-assigned identity, in the resource group's region
az identity create -g $RG -n $IDENTITY -o none
if ($?) { Write-Host "👤 Identity $IDENTITY is ready" }

# Read the identity's IDs: the resource ID to attach it, the principal ID to grant it access, and the client ID
# to point the provider at it
$IDENTITY_ID           = az identity show -g $RG -n $IDENTITY --query id -o tsv
$IDENTITY_CLIENT_ID    = az identity show -g $RG -n $IDENTITY --query clientId -o tsv
$IDENTITY_PRINCIPAL_ID = az identity show -g $RG -n $IDENTITY --query principalId -o tsv
Write-Host "🆔 Identity: $IDENTITY_ID"
Write-Host "👤 Client ID: $IDENTITY_CLIENT_ID"
Write-Host "👤 Principal ID: $IDENTITY_PRINCIPAL_ID"
```

The identity is created in the resource group's region. It can live in any resource group or subscription in
the gateway's tenant.

### Attach it to the gateway

`identity assign` adds your identity and keeps the identities the gateway already has, including the
system-assigned one. It returns when the gateway is ready again.

```bash title="Bash"
# Attach your identity to the gateway, keep its other identities, and wait until the gateway is ready again
az aigateway identity assign -g "$RG" -n "$GATEWAY" --user-assigned "$IDENTITY_ID" -o none &&
  echo "👤 Identity $IDENTITY is attached to $GATEWAY"
```

```powershell title="PowerShell"
# Attach your identity to the gateway, keep its other identities, and wait until the gateway is ready again
az aigateway identity assign -g $RG -n $GATEWAY --user-assigned $IDENTITY_ID -o none
if ($?) { Write-Host "👤 Identity $IDENTITY is attached to $GATEWAY" }
```

### Grant Foundry User on the Foundry account

```bash title="Bash"
# Give your identity the Foundry User role on the Foundry account, so that it can call the account's models
az role assignment create --assignee-object-id "$IDENTITY_PRINCIPAL_ID" --assignee-principal-type ServicePrincipal \
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "$FOUNDRY_ID" -o none &&
  echo "🔐 Identity $IDENTITY has the Foundry User role on the Foundry account"
```

```powershell title="PowerShell"
# Give your identity the Foundry User role on the Foundry account, so that it can call the account's models
az role assignment create --assignee-object-id $IDENTITY_PRINCIPAL_ID --assignee-principal-type ServicePrincipal `
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope $FOUNDRY_ID -o none
if ($?) { Write-Host "🔐 Identity $IDENTITY has the Foundry User role on the Foundry account" }
```

Grant the role on the Foundry account itself, the resource that the provider calls.

> **Checkpoint**
> `az aigateway identity show -g "$RG" -n "$GATEWAY" --query userAssignedIdentities -o json` lists your identity,
> with its `clientId` and `principalId`, and
> `az role assignment list --assignee "$IDENTITY_PRINCIPAL_ID" --scope "$FOUNDRY_ID" --query "[].roleDefinitionName" -o tsv`
> prints `Foundry User`.

<!-- track: iac | Bicep + azd -->

**What you need:** only the one-time setup from **Before you begin**. This track doesn't use your gateway from lab 02:
`azd up` creates a new one, so it works on its own.

### Choose what to declare

The templates declare the identity, its **Foundry User** role on the Foundry account, and the gateway's identities.
A deployment that declares the gateway sets its whole identity block, and the gateway keeps only the identities that
the block lists. That's why the other tracks attach the identity with `az aigateway identity assign`, which keeps the
identities the gateway already has. Here azd owns the gateway, so a template can list both the system-assigned
identity and yours.

The Foundry provider keeps the system-assigned identity for now. You point it at your identity in the next section,
which gives the role assignment time to take effect.

### Review the templates

`infra/main.bicep` creates the resource group `rg-<environment name>` and calls the quick start's
[resources.bicep](../quickstart/infra/resources.bicep) for the gateway, the Foundry model, its default alias, and the
`app-first-call` key. That provider calls Foundry with the gateway's system-assigned identity. Then `main.bicep` calls
the lab's templates for your identity:

<!-- include: infra/main.bicep -->

`infra/backend-identity.bicep` creates the user-assigned identity and calls `infra/foundry-user-role.bicep` in the
Foundry account's resource group to grant it **Foundry User**. The account can be in another resource group or
subscription, so you can also deploy this template on its own with `az deployment group create`, passing
`identityName` and `foundryAccountId`:

<!-- include: infra/backend-identity.bicep -->

<!-- include: infra/foundry-user-role.bicep -->

`infra/gateway-identity.bicep` deploys the gateway again with both identities. Every `azd provision` deploys the
quick start's gateway with only its system-assigned identity first, then attaches yours again here:

<!-- include: infra/gateway-identity.bicep -->

### Deploy with azd up

Move into the lab's folder, create an azd environment named `aigw-lab12` in your current subscription, then provision
everything. It takes about three minutes.

```bash title="Bash"
# Move into the lab's folder, which holds the azd project
cd ~/aigateway/labs/12-backend-identity &&
  echo "📂 Lab folder: $(pwd)"

# Create an azd environment in your current subscription, so that azd doesn't prompt you
azd env new aigw-lab12 --location eastus2 --subscription "$(az account show --query id --output tsv)" &&
  echo "📝 azd environment aigw-lab12 deploys to eastus2"

# Provision the gateway, the Foundry model, the alias, the key, and your identity with its role
azd up &&
  echo "🚀 Gateway deployed: $(azd env get-value AI_GATEWAY_URL)"
```

```powershell title="PowerShell"
# Move into the lab's folder, which holds the azd project
Set-Location ~/aigateway/labs/12-backend-identity
if ($?) { Write-Host "📂 Lab folder: $(Get-Location)" }

# Create an azd environment in your current subscription, so that azd doesn't prompt you
azd env new aigw-lab12 --location eastus2 --subscription (az account show --query id --output tsv)
if ($?) { Write-Host "📝 azd environment aigw-lab12 deploys to eastus2" }

# Provision the gateway, the Foundry model, the alias, the key, and your identity with its role
azd up
if ($?) { Write-Host "🚀 Gateway deployed: $(azd env get-value AI_GATEWAY_URL)" }
```

For another region, pick one where AI Gateway and the `gpt-5-mini` model are available, as in [Find the available
regions](../SETUP.md#find-the-available-regions).

### Read the outputs and the key

Set the variables that the next sections, **Verify**, and **Clean up** use from azd's environment values, and read
the key from the gateway, so that it never shows on screen. The last command lists the user-assigned identities
attached to the gateway.

```bash title="Bash"
# Read the template's outputs: the names, the runtime URL, the alias, and the identity's IDs
export RG="$(azd env get-value AZURE_RESOURCE_GROUP)"
export GATEWAY="$(azd env get-value AI_GATEWAY_NAME)"
export PROVIDER="$(azd env get-value AI_GATEWAY_PROVIDER)"
export IDENTITY="$(azd env get-value IDENTITY_NAME)"
export FOUNDRY_ID="$(azd env get-value FOUNDRY_ID)"
export AI_GATEWAY_URL="$(azd env get-value AI_GATEWAY_URL)"
export AI_GATEWAY_MODEL="$(azd env get-value AI_GATEWAY_MODEL)"
export IDENTITY_ID="$(azd env get-value IDENTITY_ID)"
export IDENTITY_CLIENT_ID="$(azd env get-value IDENTITY_CLIENT_ID)"
export IDENTITY_PRINCIPAL_ID="$(azd env get-value IDENTITY_PRINCIPAL_ID)"

# Read the key from the gateway, so that it never lands in a file or a deployment log
export AI_GATEWAY_API_KEY="$(az aigateway api-key list-secrets --ids "$(azd env get-value AI_GATEWAY_ID)/apiKeys/app-first-call" \
  --query primaryKey --output tsv)"
echo "📝 Gateway $GATEWAY in $RG, with provider $PROVIDER and identity $IDENTITY"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"
echo "🔖 Model alias: $AI_GATEWAY_MODEL"
echo "👤 Client ID: $IDENTITY_CLIENT_ID"
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"

# List the user-assigned identities attached to the gateway
echo "👤 User-assigned identities of $GATEWAY:"
az aigateway identity show -g "$RG" -n "$GATEWAY" --query userAssignedIdentities -o json
```

```powershell title="PowerShell"
# Read the template's outputs: the names, the runtime URL, the alias, and the identity's IDs
$RG                     = azd env get-value AZURE_RESOURCE_GROUP
$GATEWAY                = azd env get-value AI_GATEWAY_NAME
$PROVIDER               = azd env get-value AI_GATEWAY_PROVIDER
$IDENTITY               = azd env get-value IDENTITY_NAME
$FOUNDRY_ID             = azd env get-value FOUNDRY_ID
$env:AI_GATEWAY_URL     = azd env get-value AI_GATEWAY_URL
$env:AI_GATEWAY_MODEL   = azd env get-value AI_GATEWAY_MODEL
$IDENTITY_ID            = azd env get-value IDENTITY_ID
$IDENTITY_CLIENT_ID     = azd env get-value IDENTITY_CLIENT_ID
$IDENTITY_PRINCIPAL_ID  = azd env get-value IDENTITY_PRINCIPAL_ID

# Read the key from the gateway, so that it never lands in a file or a deployment log
$env:AI_GATEWAY_API_KEY = az aigateway api-key list-secrets --ids "$(azd env get-value AI_GATEWAY_ID)/apiKeys/app-first-call" `
  --query primaryKey --output tsv
Write-Host "📝 Gateway $GATEWAY in $RG, with provider $PROVIDER and identity $IDENTITY"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"
Write-Host "🔖 Model alias: $env:AI_GATEWAY_MODEL"
Write-Host "👤 Client ID: $IDENTITY_CLIENT_ID"
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"

# List the user-assigned identities attached to the gateway
Write-Host "👤 User-assigned identities of ${GATEWAY}:"
az aigateway identity show -g $RG -n $GATEWAY --query userAssignedIdentities -o json
```

> **Checkpoint**
> `azd up` ended with `SUCCESS`, and the list shows your identity, keyed by its resource ID, with its `clientId` and
> `principalId`.
> `az role assignment list --assignee "$IDENTITY_PRINCIPAL_ID" --scope "$FOUNDRY_ID" --query "[].roleDefinitionName" -o tsv`
> prints `Foundry User`.

<!-- track: terraform | Terraform -->

**What you need:** only the one-time setup from **Before you begin**, plus Terraform 1.9 or later, as in
[Deploy a lab with Terraform](../SETUP.md#deploy-a-lab-with-terraform). This track doesn't use your gateway from
lab 02: `terraform apply` creates a new one, so it works on its own.

### Choose what to declare

The configuration declares the identity, its **Foundry User** role on the Foundry account, and the gateway's
identities. The gateway's `identity` block lists every identity the gateway keeps, so the configuration lists both
the system-assigned identity and yours. Terraform owns the gateway here: if you attach another identity with
`az aigateway identity assign`, the next plan detaches it again.

For that reason, this lab doesn't call the quick start's [resources module](../quickstart/terraform/resources),
whose gateway lists only the system-assigned identity. `terraform/main.tf` declares the same resources itself: the
gateway, the Foundry model, its default alias, and the `app-first-call` key. The Foundry provider keeps the
system-assigned identity for now. You point it at your identity in the next section, which gives the role
assignment time to take effect.

### Review the configuration

`terraform/main.tf` creates the resource group `rg-<environment_name>`, the gateway with both identities, and the
Foundry account with its model:

<!-- include: terraform/main.tf -->

`terraform/backend-identity.tf` creates your identity and grants it **Foundry User** on the Foundry account. Set the
`identity_name` variable for another name:

<!-- include: terraform/backend-identity.tf -->

### Deploy with terraform apply

Move into the lab's Terraform folder, select your current subscription, then plan and apply the configuration. It
takes about three minutes, most of it while the new gateway activates.

```bash title="Bash"
# Move into the lab's Terraform folder
cd ~/aigateway/labs/12-backend-identity/terraform &&
  echo "📂 Terraform folder: $(pwd)"

# Deploy to the Azure CLI's current subscription
export ARM_SUBSCRIPTION_ID="$(az account show --query id --output tsv)" &&
  echo "📍 Subscription: $ARM_SUBSCRIPTION_ID"

# Download the providers, then plan and apply the gateway, the Foundry model, the alias, the key, and your identity
# with its role
terraform init &&
  terraform plan -out tfplan &&
  terraform apply tfplan &&
  echo "🚀 Gateway deployed: $(terraform output -raw ai_gateway_url)"
```

```powershell title="PowerShell"
# Move into the lab's Terraform folder
Set-Location ~/aigateway/labs/12-backend-identity/terraform
if ($?) { Write-Host "📂 Terraform folder: $(Get-Location)" }

# Deploy to the Azure CLI's current subscription
$env:ARM_SUBSCRIPTION_ID = az account show --query id --output tsv
if ($?) { Write-Host "📍 Subscription: $env:ARM_SUBSCRIPTION_ID" }

# Download the providers, then plan and apply the gateway, the Foundry model, the alias, the key, and your identity
# with its role
terraform init
if ($?) { terraform plan -out tfplan }
if ($?) { terraform apply tfplan }
if ($?) { Write-Host "🚀 Gateway deployed: $(terraform output -raw ai_gateway_url)" }
```

The configuration deploys to `eastus2`. For another region where AI Gateway and the `gpt-5-mini` model are
available, as in [Find the available regions](../SETUP.md#find-the-available-regions), run
`export TF_VAR_location=<region>` (PowerShell: `$env:TF_VAR_location = "<region>"`) before `terraform plan`.

### Read the outputs and the key

Set the variables that the next sections, **Verify**, and **Clean up** use from the Terraform outputs, and read the
key from the gateway, so that it never shows on screen. The last command lists the user-assigned identities attached
to the gateway.

```bash title="Bash"
# Read the configuration's outputs: the names, the runtime URL, the alias, and the identity's IDs
export RG="$(terraform output -raw azure_resource_group)"
export GATEWAY="$(terraform output -raw ai_gateway_name)"
export PROVIDER="$(terraform output -raw ai_gateway_provider)"
export IDENTITY="$(terraform output -raw identity_name)"
export FOUNDRY_ID="$(terraform output -raw foundry_id)"
export AI_GATEWAY_URL="$(terraform output -raw ai_gateway_url)"
export AI_GATEWAY_MODEL="$(terraform output -raw ai_gateway_model)"
export IDENTITY_ID="$(terraform output -raw identity_id)"
export IDENTITY_CLIENT_ID="$(terraform output -raw identity_client_id)"
export IDENTITY_PRINCIPAL_ID="$(terraform output -raw identity_principal_id)"

# Read the key from the gateway, so that it never lands in a file or the Terraform state's outputs
export AI_GATEWAY_API_KEY="$(az aigateway api-key list-secrets --ids "$(terraform output -raw ai_gateway_id)/apiKeys/app-first-call" \
  --query primaryKey --output tsv)"
echo "📝 Gateway $GATEWAY in $RG, with provider $PROVIDER and identity $IDENTITY"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"
echo "🔖 Model alias: $AI_GATEWAY_MODEL"
echo "👤 Client ID: $IDENTITY_CLIENT_ID"
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"

# List the user-assigned identities attached to the gateway
echo "👤 User-assigned identities of $GATEWAY:"
az aigateway identity show -g "$RG" -n "$GATEWAY" --query userAssignedIdentities -o json
```

```powershell title="PowerShell"
# Read the configuration's outputs: the names, the runtime URL, the alias, and the identity's IDs
$RG                     = terraform output -raw azure_resource_group
$GATEWAY                = terraform output -raw ai_gateway_name
$PROVIDER               = terraform output -raw ai_gateway_provider
$IDENTITY               = terraform output -raw identity_name
$FOUNDRY_ID             = terraform output -raw foundry_id
$env:AI_GATEWAY_URL     = terraform output -raw ai_gateway_url
$env:AI_GATEWAY_MODEL   = terraform output -raw ai_gateway_model
$IDENTITY_ID            = terraform output -raw identity_id
$IDENTITY_CLIENT_ID     = terraform output -raw identity_client_id
$IDENTITY_PRINCIPAL_ID  = terraform output -raw identity_principal_id

# Read the key from the gateway, so that it never lands in a file or the Terraform state's outputs
$env:AI_GATEWAY_API_KEY = az aigateway api-key list-secrets --ids "$(terraform output -raw ai_gateway_id)/apiKeys/app-first-call" `
  --query primaryKey --output tsv
Write-Host "📝 Gateway $GATEWAY in $RG, with provider $PROVIDER and identity $IDENTITY"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"
Write-Host "🔖 Model alias: $env:AI_GATEWAY_MODEL"
Write-Host "👤 Client ID: $IDENTITY_CLIENT_ID"
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"

# List the user-assigned identities attached to the gateway
Write-Host "👤 User-assigned identities of ${GATEWAY}:"
az aigateway identity show -g $RG -n $GATEWAY --query userAssignedIdentities -o json
```

> **Checkpoint**
> `terraform apply` ended with `Apply complete! Resources: 11 added, 0 changed, 0 destroyed.`, and the list shows
> your identity, keyed by its resource ID, with its `clientId` and `principalId`.
> `az role assignment list --assignee "$IDENTITY_PRINCIPAL_ID" --scope "$FOUNDRY_ID" --query "[].roleDefinitionName" -o tsv`
> prints `Foundry User`.

<!-- /tracks -->

> [!IMPORTANT]
> Role assignments can take several minutes to take effect. If you switch the provider before then, model calls
> fail with `403` from the backend until the assignment propagates.

## Point the model provider at the identity

The provider still uses the system-assigned identity. Setting `clientId` in its managed identity settings
switches it to your identity. Clients don't change: they keep calling the same alias with the same key or token.

<!-- track: portal | Portal -->

### Update the provider's authentication

The portal picks the identity only when you import a Foundry account: the **Authentication method** step offers
**Managed identity** with a choice of the system-assigned identity or any attached user-assigned identity, and
grants the selected identity **Foundry User**. To switch a provider that already exists, run this Azure CLI
command, for example in [Azure Cloud Shell](https://shell.azure.com). It sets `clientId` in the provider's
managed identity settings and keeps the rest:

```bash title="Bash"
# Set clientId to your identity's client ID; the CLI merges it into the provider's other settings
echo "🔌 Authentication of $PROVIDER:"
az aigateway model-provider update -g "$RG" --gateway-name "$GATEWAY" -n "$PROVIDER" \
  --foundry "{authentication:{managedIdentity:{clientId:$IDENTITY_CLIENT_ID}}}" \
  --query properties.authentication
```

```powershell title="PowerShell"
# Set clientId to your identity's client ID; the CLI merges it into the provider's other settings
Write-Host "🔌 Authentication of ${PROVIDER}:"
az aigateway model-provider update -g $RG --gateway-name $GATEWAY -n $PROVIDER `
  --foundry "{authentication:{managedIdentity:{clientId:$IDENTITY_CLIENT_ID}}}" `
  --query properties.authentication
```

<!-- track: cli | Azure CLI -->

### Update the provider's authentication

`--foundry` takes only the settings to change: the CLI merges them into the provider's settings, and writes
`authentication` to the top level of its properties. Set `clientId`, and the endpoint and accounts stay as they
are. Models are separate resources, so they don't change.

```bash title="Bash"
# Set clientId to your identity's client ID; the CLI merges it into the provider's other settings
echo "🔌 Authentication of $PROVIDER:"
az aigateway model-provider update -g "$RG" --gateway-name "$GATEWAY" -n "$PROVIDER" \
  --foundry "{authentication:{managedIdentity:{clientId:$IDENTITY_CLIENT_ID}}}" \
  --query properties.authentication
```

```powershell title="PowerShell"
# Set clientId to your identity's client ID; the CLI merges it into the provider's other settings
Write-Host "🔌 Authentication of ${PROVIDER}:"
az aigateway model-provider update -g $RG --gateway-name $GATEWAY -n $PROVIDER `
  --foundry "{authentication:{managedIdentity:{clientId:$IDENTITY_CLIENT_ID}}}" `
  --query properties.authentication
```

<!-- track: iac | Bicep + azd -->

### Redeploy the provider with the identity

When `useUserAssignedIdentity` is `true`, `infra/main.bicep` calls [infra/provider.bicep](infra/provider.bicep). It
redeploys the quick start's provider with the same endpoint and account, and adds `clientId` to its managed identity
settings. `authentication` sits at the top level of the provider's properties, and the setting it holds,
`managedIdentity` or `headers`, picks the method:

```bicep
authentication: {
  managedIdentity: {
    resource: 'https://cognitiveservices.azure.com/'
    clientId: identityClientId   // omit clientId to use the system-assigned identity
  }
}
```

A deployment replaces the provider's properties, so the template sets all of them, including the display name, which
is required, and the description. Models and aliases are separate resources and aren't touched. You can also deploy
`infra/provider.bicep` on its own to a gateway that lab 02's Bicep track created, with `az deployment group create`,
passing `gatewayName`, `providerName`, `foundryAccountId`, and `identityClientId`. If you created the provider in
the portal or with the CLI, use the **Azure CLI** track's commands instead, which change only the authentication.

<!-- include: infra/provider.bicep -->

azd reads the parameter from `USE_USER_ASSIGNED_IDENTITY`. Set it, provision again, and read the provider's
authentication settings:

```bash title="Bash"
# Tell azd to point the Foundry provider at your identity
azd env set USE_USER_ASSIGNED_IDENTITY true &&
  echo "📝 USE_USER_ASSIGNED_IDENTITY is true"

# Provision again, which now redeploys the provider with infra/provider.bicep
azd provision &&
  echo "🚀 Provider $PROVIDER is redeployed with identity $IDENTITY"

# Show the provider's authentication settings
echo "🔌 Authentication of $PROVIDER:"
az aigateway model-provider show -g "$RG" --gateway-name "$GATEWAY" -n "$PROVIDER" \
  --query properties.authentication -o json
```

```powershell title="PowerShell"
# Tell azd to point the Foundry provider at your identity
azd env set USE_USER_ASSIGNED_IDENTITY true
if ($?) { Write-Host "📝 USE_USER_ASSIGNED_IDENTITY is true" }

# Provision again, which now redeploys the provider with infra/provider.bicep
azd provision
if ($?) { Write-Host "🚀 Provider $PROVIDER is redeployed with identity $IDENTITY" }

# Show the provider's authentication settings
Write-Host "🔌 Authentication of ${PROVIDER}:"
az aigateway model-provider show -g $RG --gateway-name $GATEWAY -n $PROVIDER `
  --query properties.authentication -o json
```

Each `azd provision` deploys the quick start's gateway and provider first, with the system-assigned identity, then
attaches your identity and points the provider at it again. In between, model calls can fail for a few seconds with
`500`, so don't provision again while the gateway serves real traffic. To switch back, set
`USE_USER_ASSIGNED_IDENTITY` to `false` and run `azd provision`.

<!-- track: terraform | Terraform -->

### Update the provider with the identity

`terraform/model-provider.tf` declares the Foundry provider. When the `use_user_assigned_identity` variable is
`true`, it adds your identity's `clientId` to the provider's managed identity settings. Otherwise `clientId` is
`null`, which leaves it out, so the provider uses the system-assigned identity:

<!-- include: terraform/model-provider.tf -->

Plan the change with the variable, apply the plan, and read the provider's authentication settings:

```bash title="Bash"
# Plan the change: point the Foundry provider at your identity
terraform plan -var use_user_assigned_identity=true -out tfplan &&
  echo "📝 The plan adds clientId to the provider's managed identity settings"

# Apply the plan, which updates only the provider
terraform apply tfplan &&
  echo "🚀 Provider $PROVIDER uses identity $IDENTITY"

# Show the provider's authentication settings
echo "🔌 Authentication of $PROVIDER:"
az aigateway model-provider show -g "$RG" --gateway-name "$GATEWAY" -n "$PROVIDER" \
  --query properties.authentication -o json
```

```powershell title="PowerShell"
# Plan the change: point the Foundry provider at your identity
terraform plan -var use_user_assigned_identity=true -out tfplan
if ($?) { Write-Host "📝 The plan adds clientId to the provider's managed identity settings" }

# Apply the plan, which updates only the provider
terraform apply tfplan
if ($?) { Write-Host "🚀 Provider $PROVIDER uses identity $IDENTITY" }

# Show the provider's authentication settings
Write-Host "🔌 Authentication of ${PROVIDER}:"
az aigateway model-provider show -g $RG --gateway-name $GATEWAY -n $PROVIDER `
  --query properties.authentication -o json
```

The plan ends with `Plan: 0 to add, 1 to change, 0 to destroy.`: Terraform updates the provider in place, and the
gateway, models, and aliases don't change. Pass the same `-var` to every later plan. A plan without it switches the
provider back to the system-assigned identity, with the same one change.

<!-- /tracks -->

> **Checkpoint**
> The CLI output shows a `managedIdentity` with `clientId` equal to `$IDENTITY_CLIENT_ID` and `resource` set to
> `https://cognitiveservices.azure.com/`: the provider's `properties.authentication.managedIdentity.clientId`.

## Verify

<!-- steps -->

### Confirm which identity the provider uses

```bash title="Bash"
# Read the client ID that the provider signs in with, and compare it with your identity's client ID
PROVIDER_CLIENT_ID="$(az aigateway model-provider show -g "$RG" --gateway-name "$GATEWAY" -n "$PROVIDER" \
  --query properties.authentication.managedIdentity.clientId -o tsv)"
echo "🔌 Provider's client ID: $PROVIDER_CLIENT_ID"
echo "👤 Identity's client ID: $IDENTITY_CLIENT_ID"
```

```powershell title="PowerShell"
# Read the client ID that the provider signs in with, and compare it with your identity's client ID
$PROVIDER_CLIENT_ID = az aigateway model-provider show -g $RG --gateway-name $GATEWAY -n $PROVIDER `
  --query properties.authentication.managedIdentity.clientId -o tsv
Write-Host "🔌 Provider's client ID: $PROVIDER_CLIENT_ID"
Write-Host "👤 Identity's client ID: $IDENTITY_CLIENT_ID"
```

Both lines print the same client ID. In the portal, **Managed identities** › **View access** now lists your
provider under your identity's name instead of **System-assigned**. The list reflects the configured binding,
not the live role assignment on the Foundry account, so a missing role still shows up only as a failed call.

### Call the model through the gateway

```bash title="Bash"
# Call the alias through the gateway with your runtime key, and print only the HTTP status code
echo "🤖 HTTP status of a call to $AI_GATEWAY_MODEL through the gateway:"
curl -sS -o /dev/null -w "HTTP %{http_code}\n" "${AI_GATEWAY_URL%/}/default/models/openai/v1/chat/completions" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
  -d "{\"model\":\"$AI_GATEWAY_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}"
```

```powershell title="PowerShell"
# Build a one-message request for the alias
$body = @{ model = $env:AI_GATEWAY_MODEL; messages = @(@{ role = "user"; content = "ping" }) } | ConvertTo-Json -Depth 5

# Call the alias through the gateway with your runtime key, and print only the HTTP status code
Write-Host "🤖 HTTP status of a call to $env:AI_GATEWAY_MODEL through the gateway:"
(Invoke-WebRequest -Method Post -Uri "$($env:AI_GATEWAY_URL.TrimEnd('/'))/default/models/openai/v1/chat/completions" `
  -Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body -SkipHttpErrorCheck).StatusCode
```

Expect `HTTP 200`. A `403` that mentions permissions on the Foundry account means the role assignment hasn't
propagated yet: wait a few minutes and retry.

You called the same alias with the same key as in lab 02. Only the gateway's outbound credential changed, and no
backend key was stored or sent.

<!-- /steps -->

> **Checkpoint**
> The provider names your user-assigned identity, **View access** shows the new binding, and the model still
> answers with `200`.

## Go further

### Use stored credentials when a backend can't take a managed identity

Managed identity needs a backend that accepts Microsoft Entra tokens. When it doesn't, store the backend's own
credential in the gateway, never in the calling app.

| Backend | How the gateway authenticates |
|---|---|
| Microsoft Foundry and Azure OpenAI | **Managed identity** (recommended) or **API key**, chosen in the import's **Authentication method** step |
| OpenAI, Anthropic, AWS Bedrock, Google Vertex AI, and other custom models | The provider's API key, entered when you add the custom model ([lab 03](../03-multi-provider-aliases/README.md)) |
| MCP server backends | **None**, **API key**, **OAuth 2.0**, or **Managed identity**, set per backend ([lab 04](../04-federate-mcp-tools/README.md)) |

- Prefer managed identity whenever the backend supports it.
- The gateway stores backend keys and tokens with the asset or connection and never returns them to clients.
- Rotate backend credentials on their own schedule. They're independent of the runtime keys callers use.

At API version `2026-11-01`, a provider stores its key as one header in `authentication.headers`, in place of
`managedIdentity`, for example `"authentication": { "headers": [ { "name": "api-key", "value": "<foundry-key>" } ] }`
for Foundry. A read returns only the header's `name`, never its `value`. So a script that reads a provider and writes
it back must send the key again, or the provider loses it.

### Call MCP backends as the signed-in user

A managed identity calls a backend as the gateway, so every caller gets the gateway's access. Some MCP backends,
such as an expenses API, must instead see who the user is and return only that user's data. For those, an MCP
server backend can use **OAuth 2.0** with the **On behalf of** profile: the client signs the user in with a
Microsoft Entra token for the gateway, and the gateway exchanges it for a delegated token for the backend. A token
exchange app registration holds the delegated permissions, and a user-assigned identity attached to the gateway, like
the one in this lab, authenticates that app through a federated credential, so no client secret is stored. See
[Access MCP server backends with user credentials](https://ai.gateway.azure.com/docs/toolservers/access-backends-with-user-credentials).
[33 · Connect Microsoft 365 Copilot to MCP tools as the signed-in user](../33-copilot-obo-mcp/README.md) builds the whole
chain, from Copilot Cowork to an MCP server on Azure Container Apps.

### Drop access the system identity no longer needs

After every asset that calls the Foundry account uses the user-assigned identity, the system-assigned identity's
**Foundry User** assignment on that account is unused. Check **View access** › **Backend access** first: remove
the assignment only if no other asset lists **System-assigned** for that account, because later labs in this
series use it. A call that still returns `200` a few minutes after you remove it also proves that the provider
uses the user-assigned identity.

## Clean up

Pick the track you followed.

<!-- track: cli | Portal and Azure CLI -->

Keep the system-assigned identity and its **Foundry User** role. Later labs use them. Remove the user-assigned
identity in this order, so the provider never points at an identity it can't use.

### Switch the provider back to the system-assigned identity

Removing `clientId` selects the system-assigned identity again. A `null` value removes a setting:

```bash title="Bash"
# Remove clientId (null removes a setting), so the provider signs in with the system-assigned identity again
echo "🔌 Authentication of $PROVIDER:"
az aigateway model-provider update -g "$RG" --gateway-name "$GATEWAY" -n "$PROVIDER" \
  --foundry "{authentication:{managedIdentity:{clientId:null}}}" \
  --query properties.authentication
```

```powershell title="PowerShell"
# Remove clientId (null removes a setting), so the provider signs in with the system-assigned identity again
Write-Host "🔌 Authentication of ${PROVIDER}:"
az aigateway model-provider update -g $RG --gateway-name $GATEWAY -n $PROVIDER `
  --foundry "{authentication:{managedIdentity:{clientId:null}}}" `
  --query properties.authentication
```

The output no longer shows `clientId`.

### Detach the identity from the gateway

Remove your identity from the gateway. `identity remove` returns when the gateway is ready again:

```bash title="Bash"
# Detach your identity from the gateway, and wait until the gateway is ready again
az aigateway identity remove -g "$RG" -n "$GATEWAY" --user-assigned "$IDENTITY_ID" -o none &&
  echo "🚮 Identity $IDENTITY is detached from $GATEWAY"
```

```powershell title="PowerShell"
# Detach your identity from the gateway, and wait until the gateway is ready again
az aigateway identity remove -g $RG -n $GATEWAY --user-assigned $IDENTITY_ID -o none
if ($?) { Write-Host "🚮 Identity $IDENTITY is detached from $GATEWAY" }
```

This detaches only your identity. The system-assigned identity and any other user-assigned identities stay
attached.

In the portal, select the identity's checkbox in **User-assigned identities**, select the trash icon in the
selection bar, then **Detach**. The confirmation says that detaching removes the identity from this gateway only: the
identity and its role assignments stay.

### Remove the role assignment and delete the identity

```bash title="Bash"
# Remove your identity's Foundry User role assignment from the Foundry account
az role assignment delete --assignee "$IDENTITY_PRINCIPAL_ID" --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "$FOUNDRY_ID" &&
  echo "🚮 The Foundry User role assignment of $IDENTITY is deleted"

# Delete the identity, now that nothing uses it
az identity delete -g "$RG" -n "$IDENTITY" &&
  echo "🚮 Identity $IDENTITY is deleted"
```

```powershell title="PowerShell"
# Remove your identity's Foundry User role assignment from the Foundry account
az role assignment delete --assignee $IDENTITY_PRINCIPAL_ID --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope $FOUNDRY_ID
if ($?) { Write-Host "🚮 The Foundry User role assignment of $IDENTITY is deleted" }

# Delete the identity, now that nothing uses it
az identity delete -g $RG -n $IDENTITY
if ($?) { Write-Host "🚮 Identity $IDENTITY is deleted" }
```

Delete the role assignment first. Deleting the identity alone leaves an orphaned assignment on the account.

> **Checkpoint**
> The model call from **Verify** still returns `200`, and **View access** shows the provider under
> **System-assigned** again.

<!-- track: iac | Bicep + azd -->

### Delete everything azd created

In the `aigateway/labs/12-backend-identity` folder, delete the resource group with the gateway, the Foundry account,
and the identity with its role assignment. [`--purge`](../SETUP.md#delete-what-azd-created) also purges the
soft-deleted Foundry account. It takes a few minutes.

```bash
# Delete everything that azd up created, and purge the Foundry account
azd down --purge --force &&
  echo "🚮 Everything that azd up created is deleted"
```

<!-- track: terraform | Terraform -->

### Delete everything Terraform created

In the `aigateway/labs/12-backend-identity/terraform` folder, delete the resource group with the gateway, the Foundry
account, and the identity with its role assignment. The [`azurerm`
provider](../SETUP.md#delete-what-terraform-created) also purges the soft-deleted Foundry account. It takes a few
minutes.

```bash
# Delete everything that terraform apply created, and purge the soft-deleted Foundry account
cd ~/aigateway/labs/12-backend-identity/terraform &&
  terraform destroy -auto-approve &&
  echo "🚮 Everything that terraform apply created is deleted"
```

<!-- /tracks -->

`azd down` and `terraform destroy` don't touch your gateway from lab 02 or its system-assigned identity, because the
Bicep + azd and Terraform tracks created their own gateway.

## Next steps

- [Go keyless with Microsoft Entra ID and RBAC](../11-entra-rbac/README.md): remove keys on the inbound side too.
- [Federate MCP tools behind one endpoint](../04-federate-mcp-tools/README.md): set backend authentication for tool servers.
- [Lock down backend traffic with private networking](../18-private-networking/README.md): keep backend traffic off the public internet.
- Docs: [Security and identity](https://ai.gateway.azure.com/docs/security-identity) and
  [Microsoft Foundry and AI Gateway](https://ai.gateway.azure.com/docs/foundry).
