TL;DR
- After the client secret is revoked, a new token request fails with invalid_client, but tokens already issued still read users.
- Disabling the service principal stopped Microsoft Graph accepting those tokens within about two minutes.
- Removing the app's role did not affect an issued token during the three minutes I watched.
- A client that asks for it gets a token that lasts about a day, and a managed identity token does too.
- Deleting a managed identity cut off its token in under two minutes.
Repeat this lab
I wrote this lab so you can run it yourself. Each step has the commands to copy and the output I got, and you can tick it as done. Your ticks stay in this browser only.
Copies a ready-made prompt for Claude Code, Cursor, Codex or any coding agent. For test environments only.
Show prompt
I want to run a hands-on lab from iamops.dev, and I would like your help to do it safely. Lab: Revoking a client secret does not stop the token it already issued Read the whole lab here (Markdown): https://iamops.dev/entra-revoked-secret-token-still-works.md What the lab needs before the first step: - Throwaway tenant, Azure CLI signed in with az login --tenant "$TENANT_ID" --allow-no-subscriptions - Values I have to set myself (the lab uses placeholders): <TENANT_ID> How I want you to work: 1. Read the lab from start to finish first (each step lists its own prerequisites). Then check that the prerequisites are in place and that the tool versions match what the lab expects. Tell me what is missing before you run anything. 2. Run one step at a time. Before each command, show it to me and say in a sentence what it does. 3. Ask me before anything that costs money, creates cloud resources, touches a real tenant or account, or deletes anything. Wait for my answer. 4. Use the placeholders from the lab. Never paste real secrets, tokens or production identities into a command, a file or this chat. If a value is needed, tell me where to set it myself. 5. After each step, compare the output with the expected output in the lab. If it does not match, stop and tell me what differs. Do not carry on or invent a fix without asking me. 6. At the end, report what passed, what failed and which cleanup you did. Then offer to tick the steps off for me. The lab page keeps ticks only in my browser, so if you cannot do that, list the steps I should tick. This lab is for test environments only. Do not run it against production, or against a tenant or account that holds real data or real users. You may answer in my language.
The answer: revoking the secret does not. It stops new tokens, and the ones already issued keep reading data from Microsoft Graph. In my runs, disabling the app’s service principal stopped them within about two minutes.
Setup#
You need a throwaway tenant, the Azure command-line interface (CLI), curl and jq. Never use a work or production tenant. Sign in with az login --tenant "$TENANT_ID" --allow-no-subscriptions and set two variables:
export TENANT_ID=<TENANT_ID>
export APP_NAME=lab-revoke-token-testCreate the app and its service principal:
export APP_ID=$(az ad app create --display-name "$APP_NAME" --sign-in-audience AzureADMyOrg --query appId -o tsv)
az ad sp create --id "$APP_ID" --query id -o tsv
echo "$APP_ID"The app needs one permission for the test to mean anything: User.Read.All, an application permission on Microsoft Graph. I assigned the role directly to the service principal, because admin consent through the CLI left my tokens without it:
GRAPH_ID=$(az ad sp list --display-name "Microsoft Graph" --query "[0].appId" -o tsv)
READ_ROLE=$(az ad sp show --id "$GRAPH_ID" --query "appRoles[?value=='User.Read.All'].id | [0]" -o tsv)
az ad app permission add --id "$APP_ID" --api "$GRAPH_ID" --api-permissions "$READ_ROLE=Role"
APP_SP=$(az ad sp show --id "$APP_ID" --query id -o tsv)
GRAPH_SP=$(az ad sp show --id "$GRAPH_ID" --query id -o tsv)
az rest --method post --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APP_SP/appRoleAssignments" --headers "Content-Type=application/json" --body "{\"principalId\":\"$APP_SP\",\"resourceId\":\"$GRAPH_SP\",\"appRoleId\":\"$READ_ROLE\"}"
az ad app permission list --id "$APP_ID" --query "[].resourceAccess[].type" -o tsv
sleep 60Then create a client secret that expires in seven days, written to a file only your user can read:
umask 077
END=$(date -u -v+7d +%Y-%m-%d)
az ad app credential reset --id "$APP_ID" --append --display-name lab --end-date "$END" --query password -o tsv > secret.value
printf 'ENTRA_APP_SECRET=%s\n' "$(cat secret.value)" > secret.env
rm -f secret.value
source ./secret.envGet a token#
The client credentials flow needs no user sign-in. The token stays in a shell variable, and only a few claims are printed:
TOKEN=$(curl -s -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$APP_ID" \
--data-urlencode "client_secret=$ENTRA_APP_SECRET" \
--data-urlencode scope=https://graph.microsoft.com/.default | jq -r .access_token)
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | jq -R '@base64d | fromjson | {aud, idtyp, roles, lifetime_seconds: (.exp - .iat)}'
iat includes five minutes of clock allowance.A token that lasts a day#
An app can tell Entra that it handles CAE, by sending the cp1 client capability. The same secret then buys a token that lasts about 24 hours. Whoever holds the secret decides whether to ask for it.
curl -s -o long.json -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$APP_ID" \
--data-urlencode "client_secret=$ENTRA_APP_SECRET" \
--data-urlencode scope=https://graph.microsoft.com/.default \
--data-urlencode 'claims={"access_token":{"xms_cc":{"values":["cp1"]}}}'
jq '{expires_in}' long.json
LONG_TOKEN=$(jq -r .access_token long.json); rm -f long.jsonRead users before the revoke#
I called Graph with both tokens. A small function prints the status and the number of users, so no names or IDs leave the terminal:
users() { curl -s -o resp.json -w "$1 HTTP %{http_code} " -H "Authorization: Bearer $2" "https://graph.microsoft.com/v1.0/users?\$top=5&\$select=id"; jq -r '.error.code // "users: \(.value | length)"' resp.json; }
users short "$TOKEN"
users long "$LONG_TOKEN"Both returned HTTP 200 and five users.
Revoke the secret#
This is the step a team takes after a leak:
KEY_ID=$(az ad app show --id "$APP_ID" --query "passwordCredentials[0].keyId" -o tsv)
az ad app credential delete --id "$APP_ID" --key-id "$KEY_ID"
echo "secrets left: $(az ad app show --id "$APP_ID" --query "length(passwordCredentials)" -o tsv)"After the revoke#
First a new token request with the old secret, then both old tokens:
curl -s -o new.json -w "HTTP %{http_code}\n" -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$APP_ID" \
--data-urlencode "client_secret=$ENTRA_APP_SECRET" \
--data-urlencode scope=https://graph.microsoft.com/.default
jq -r '"error=\(.error)"' new.json
users short "$TOKEN"
users long "$LONG_TOKEN"
The new request got HTTP 401 with invalid_client. Both old tokens got HTTP 200.
Disable the service principal#
Disabling sign-in for the service principal is a different switch. I set it and read users with both tokens every 30 seconds:
APP_SP=$(az ad sp show --id "$APP_ID" --query id -o tsv)
az rest --method patch --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APP_SP" \
--headers "Content-Type=application/json" --body '{"accountEnabled":false}'
for i in $(seq 1 10); do
date -u +%H:%M:%S
users short "$TOKEN"
users long "$LONG_TOKEN"
sleep 30
done
Graph refused the ordinary token after about one minute and the long-lived token after about two, with Authorization_IdentityDisabled. An earlier run refused both after about 90 seconds. Microsoft documents this revocation for CAE tokens on Microsoft Graph; in my runs Graph refused the ordinary token too. An API that only checks a token’s signature and expiry has no way to know the app was disabled, so test your own APIs.
Limit the risk#
A leak does not need a mistake in a repository: infostealers now copy the tokens and API keys of AI coding tools straight from developer laptops. If a secret leaks, do these in order: disable the service principal, remove the secret, then look at the sign-in log before you enable the app again.
- Disable the service principal. Stops new tokens and, on Microsoft Graph, the issued ones. It also stops the real app, so plan the outage.
- Remove the secret. Stops new tokens only. Needed anyway, so the leaked value is useless after you re-enable the app.
- Remove the app’s role. An issued token carries its roles inside it. After I removed the assignment, both tokens still read users for the three minutes I watched.
- Delete the app. Also ends access, though answers alternated for a short while in my run. It is harder to undo than disabling.
- Short secret expiry. Limits how long a forgotten secret works. A one-day secret behaved like the seven-day one: revoking it left the issued token working.
- Token lifetime policy. The policy follows the API that receives the token, not the app that asks for it. A ten-minute policy on my client app did not change its Graph token. Set it on your own APIs.
- No secret at all. A federated credential or a managed identity leaves nothing to copy from a file or a repository. A stolen token is still a token.
- Watch the sign-ins. Service principal sign-ins appear in the Graph sign-in log with the caller’s IP address, a few minutes late. Reading it needs Entra ID P1 or higher. Alert on an address the app never used before.
- Restrict where the app signs in from. Conditional Access for workload identities can block by location. It needs Workload Identities Premium.
Managed identity, with no secret#
I repeated the question with a user-assigned managed identity on an Azure Container Instance, with the same read role. A small probe script inside the container kept the first token and used it every 30 seconds.
az group create -n rg-lab-revoke-mi -l <REGION>
az identity create -g rg-lab-revoke-mi -n lab-revoke-mi
# assign User.Read.All to the identity's principalId, as in the setup above
az container create -g rg-lab-revoke-mi -n lab-revoke-probe --image mcr.microsoft.com/azurelinux/base/python:3.12 \
--os-type Linux --cpu 1 --memory 1 --restart-policy Never --assign-identity <IDENTITY_ID> \
--environment-variables MI_CLIENT_ID=<CLIENT_ID> PROBE="$(base64 < probe.py | tr -d '\n')" \
--command-line "python3 -c 'import os,base64;exec(base64.b64decode(os.environ[\"PROBE\"]))'"
az identity delete -g rg-lab-revoke-mi -n lab-revoke-mi
az container logs -g rg-lab-revoke-mi -n lab-revoke-probe
az group delete -n rg-lab-revoke-mi --yes
The token’s lifetime was 86,700 seconds. Graph refused it right after the deletion, let one more call through about a minute later, and refused every call from about 95 seconds on. For several minutes the identity endpoint kept returning the same cached token, then it started returning errors. Microsoft states that CAE for workload identities does not cover managed identities, so treat this as what I observed on Graph, not as a promise.
Cleanup#
Delete the app and its service principal, then check with separate queries:
az ad app delete --id "$APP_ID"
az ad app list --display-name "$APP_NAME" --query "length(@)" -o tsv
az ad sp list --display-name "$APP_NAME" --query "length(@)" -o tsv
rm -f secret.env new.json resp.json
unset TOKEN LONG_TOKEN ENTRA_APP_SECRET
Entra keeps a deleted app for 30 days, so it can still be restored from the portal.
Repeat it#
The lab steps run the same commands in order, with the expected output after each one. I ran them in one session before publishing. Keep the values in your shell, and never print the secret or a token.
Written by Argo. The runs were on 29 November 2026 in a personal Entra test tenant, with apps and one identity created for the lab and one read permission. All of them were removed afterwards. The values in the commands are placeholders.
Repeat this lab#
Needs a throwaway Entra tenant, the Azure CLI, curl and jq, and an account that can create app registrations and assign Microsoft Graph application roles. It creates one app with one read permission (User.Read.All) and one client secret, gets two tokens, revokes the secret, shows that both tokens still read users, then disables the service principal and watches them stop. About 15 minutes. Never use a work or production tenant.
Every value in angle brackets is a placeholder for your own test environment. Nothing on this page is a real identifier or secret.
Set your variables once
export TENANT_ID=<TENANT_ID>
export APP_NAME=lab-revoke-token-test 0 of 10 steps done
Step 1Create the app and its service principal
Goal. One app registration and the service principal that lets it sign in.
Prerequisites
- Throwaway tenant, Azure CLI signed in with az login --tenant "$TENANT_ID" --allow-no-subscriptions
export APP_ID=$(az ad app create --display-name "$APP_NAME" --sign-in-audience AzureADMyOrg --query appId -o tsv)
az ad sp create --id "$APP_ID" --query id -o tsv
echo "$APP_ID" Expected output
<SERVICE_PRINCIPAL_ID>
<APP_ID> Step 2Add the Graph read permission and assign the role
Goal. User.Read.All as an application permission, looked up by name, then assigned to the service principal. Wait a minute before asking for a token so the assignment is in place.
Prerequisites
- The app step printed an APP_ID
GRAPH_ID=$(az ad sp list --display-name "Microsoft Graph" --query "[0].appId" -o tsv)
READ_ROLE=$(az ad sp show --id "$GRAPH_ID" --query "appRoles[?value=='User.Read.All'].id | [0]" -o tsv)
az ad app permission add --id "$APP_ID" --api "$GRAPH_ID" --api-permissions "$READ_ROLE=Role"
APP_SP=$(az ad sp show --id "$APP_ID" --query id -o tsv)
GRAPH_SP=$(az ad sp show --id "$GRAPH_ID" --query id -o tsv)
az rest --method post --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APP_SP/appRoleAssignments" --headers "Content-Type=application/json" --body "{\"principalId\":\"$APP_SP\",\"resourceId\":\"$GRAPH_SP\",\"appRoleId\":\"$READ_ROLE\"}"
az ad app permission list --id "$APP_ID" --query "[].resourceAccess[].type" -o tsv
sleep 60 Expected output
Role Step 3Create a 7-day client secret
Goal. A client secret the app can use to ask for a token. Keep the value in a file only you can read; never print it.
Prerequisites
- The app step printed an APP_ID
umask 077
END=$(date -u -v+7d +%Y-%m-%d)
az ad app credential reset --id "$APP_ID" --append --display-name lab --end-date "$END" --query password -o tsv > secret.value
printf 'ENTRA_APP_SECRET=%s\n' "$(cat secret.value)" > secret.env
rm -f secret.value
source ./secret.env Expected output
(no output: the value is written to secret.env, mode 600) Step 4Request a token and read its claims
Goal. The client-credentials request, then the claims of the access token. The roles claim should list User.Read.All. The token itself stays in a shell variable.
Prerequisites
- The secret step sourced secret.env
TOKEN=$(curl -s -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$APP_ID" \
--data-urlencode "client_secret=$ENTRA_APP_SECRET" \
--data-urlencode scope=https://graph.microsoft.com/.default | jq -r .access_token)
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | jq -R '@base64d | fromjson | {aud, idtyp, roles, lifetime_seconds: (.exp - .iat)}' Expected output
{
"aud": "https://graph.microsoft.com",
"idtyp": "app",
"roles": [
"User.Read.All"
],
"lifetime_seconds": 3900
} Step 5Ask for a long-lived token
Goal. The same request with the cp1 client capability, which opts in to Continuous Access Evaluation. Entra answers with a token that lasts about a day.
Prerequisites
- The secret step sourced secret.env
curl -s -o long.json -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$APP_ID" \
--data-urlencode "client_secret=$ENTRA_APP_SECRET" \
--data-urlencode scope=https://graph.microsoft.com/.default \
--data-urlencode 'claims={"access_token":{"xms_cc":{"values":["cp1"]}}}'
jq '{expires_in}' long.json
LONG_TOKEN=$(jq -r .access_token long.json); rm -f long.json Expected output
{
"expires_in": 86399
} Step 6Read users with both tokens before revoking
Goal. Call Microsoft Graph with each token. Only the count is printed.
Prerequisites
- The token step set TOKEN
users() { curl -s -o resp.json -w "$1 HTTP %{http_code} " -H "Authorization: Bearer $2" "https://graph.microsoft.com/v1.0/users?\$top=5&\$select=id"; jq -r '.error.code // "users: \(.value | length)"' resp.json; }
users short "$TOKEN"
users long "$LONG_TOKEN" Expected output
short HTTP 200 users: 5
long HTTP 200 users: 5 Step 7Revoke the secret
Goal. Delete the client secret. This is the step a team takes after a leak.
Prerequisites
- The secret step created a secret for APP_ID
KEY_ID=$(az ad app show --id "$APP_ID" --query "passwordCredentials[0].keyId" -o tsv)
az ad app credential delete --id "$APP_ID" --key-id "$KEY_ID"
echo "secrets left: $(az ad app show --id "$APP_ID" --query "length(passwordCredentials)" -o tsv)" Expected output
secrets left: 0 Step 8Ask for a new token, then read users with the old ones
Goal. A new token needs the secret, so it fails. The tokens issued before the revoke still read users.
Prerequisites
- The revoke step removed the secret
curl -s -o new.json -w "HTTP %{http_code}\n" -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$APP_ID" \
--data-urlencode "client_secret=$ENTRA_APP_SECRET" \
--data-urlencode scope=https://graph.microsoft.com/.default
jq -r '"error=\(.error)"' new.json
users short "$TOKEN"
users long "$LONG_TOKEN" Expected output
HTTP 401
error=invalid_client
short HTTP 200 users: 5
long HTTP 200 users: 5 Step 9Disable the service principal and watch both tokens
Goal. Switch off sign-in for the app's service principal, then read users every 30 seconds. Answers can alternate for a minute or two while the change spreads.
Prerequisites
- The after-revoke step showed both tokens still working
APP_SP=$(az ad sp show --id "$APP_ID" --query id -o tsv)
az rest --method patch --uri "https://graph.microsoft.com/v1.0/servicePrincipals/$APP_SP" \
--headers "Content-Type=application/json" --body '{"accountEnabled":false}'
for i in $(seq 1 10); do
date -u +%H:%M:%S
users short "$TOKEN"
users long "$LONG_TOKEN"
sleep 30
done Expected output
01:25:03
short HTTP 200 users: 5
long HTTP 200 users: 5
01:25:33
short HTTP 200 users: 5
long HTTP 200 users: 5
01:26:04
short HTTP 401 Authorization_IdentityDisabled
long HTTP 200 users: 5
...
01:29:36
short HTTP 401 Authorization_IdentityDisabled
long HTTP 401 InvalidAuthenticationToken Step 10Delete the app and check nothing is left
Goal. Delete the app registration and its service principal, then check with separate queries.
Prerequisites
- The app step created APP_ID
az ad app delete --id "$APP_ID"
az ad app list --display-name "$APP_NAME" --query "length(@)" -o tsv
az ad sp list --display-name "$APP_NAME" --query "length(@)" -o tsv
rm -f secret.env new.json resp.json
unset TOKEN LONG_TOKEN ENTRA_APP_SECRET Expected output
0
0 Sources#
This post as Markdown · RSS · JSON feed
New posts by email
One email a week with new posts and labs. Unsubscribe anytime.
Your address is stored with Kit, which sends the emails. Details