Device code flow: blocking it with Conditional Access
I kept reading about attacks where the victim types a code into a genuine Microsoft page and unknowingly hands over access to their account. What caught my attention is that there is no fake page to spot. Here I put in order how the device code flow works and how I blocked it with Conditional Access in my test tenant.
In brief
- Device code flow was built for devices without a keyboard, but it can be abused for phishing
- The
Authentication flowscondition in Conditional Access blocks or limits it - Start in report-only mode, then switch it on
Prerequisites: Microsoft Entra ID P1 licence, Conditional Access Administrator role (licence and procedure checked on Microsoft Learn, October 2026).
How device code flow works
The flow targets input-constrained devices such as smart TVs, printers, IoT devices, shared screens or digital signage. The device requests a code from the /devicecode endpoint and shows the user a user_code and a URL. The user opens that URL on another device, enters the code and signs in.
Meanwhile the client polls the /token endpoint with the device_code until the user is done. If the request includes offline_access, a refresh token is returned as well.
Why it is a problem
Anyone can start the flow. An attacker generates the code, sends it to the victim with a pretext and asks them to enter it on the real Microsoft sign-in page. The victim authenticates, MFA included, and the token ends up with the attacker's client. Microsoft calls it a high-risk flow and recommends blocking it wherever possible.
π‘ The code is valid only for a limited time (15 minutes by default for
expires_in), but the token it yields lives much longer: the damage doesn't end when the code expires.
Before blocking: find out who uses it
A blind block can break legitimate tools, for example Teams devices or scripts using Azure CLI. The AuthenticationProtocol column in the sign-in logs holds the value deviceCode, so a query gives you an inventory.
union SigninLogs, AADNonInteractiveUserSignInLogs
| where TimeGenerated > ago(30d)
| where AuthenticationProtocol == "deviceCode"
| summarize Signins = count(), LastSeen = max(TimeGenerated)
by UserPrincipalName, AppDisplayName, ResourceDisplayName, IPAddress
| order by Signins descFor every row you get back, ask who the user is, which app is involved and who owns the business need.
The Conditional Access policy
This is the policy Microsoft documents for organisations that don't use device code flow.
π entra.microsoft.com β Entra ID β Conditional Access β Policies β New policy
| Setting | Value |
|---|---|
| Users, Include | All users |
| Users, Exclude | emergency access (break-glass) accounts and approved exception groups |
| Target resources | All resources |
| Conditions β Authentication flows | Device code flow |
| Grant | Block access |
| Enable policy | Report-only, then On |
π‘ Portal menus change often: if the names differ, look for the
Authentication flowscondition.
Exceptions and surprises
Two points I wouldn't have guessed. First: if the policy covers All resources and you use device code flow to register devices, you must exclude the Device Registration Service resource, otherwise registration gets blocked.
Second is protocol tracking: a session started with device code flow stays flagged on later refreshes. A sign-in can therefore be blocked even when the event's own protocol isn't deviceCode. In the logs you spot it through the Original transfer method field and, with a policy on all apps, through the AADSTS530036 error.
Conclusions
The block is a single policy, but the real work is the inventory beforehand. I'm left wondering how many tenants still carry Β«temporaryΒ» exceptions nobody ever reviewed.