Block Device Code Flow with Conditional Access
Device code flow lets someone sign in on a device without a keyboard by entering a code on another device. Microsoft calls it a high-risk authentication method that can be part of a phishing attack, and recommends blocking it wherever possible and allowing it only where necessary (Authentication flows (opens in a new tab)). Conditional Access has a dedicated Authentication flows condition for this.
Use it when: you're hardening a tenant against token phishing, or a security review flags device code sign-ins you can't explain.
Before you start
- License: Conditional Access requires Microsoft Entra ID P1 (Licensing (opens in a new tab)).
- Role: at least Conditional Access Administrator to create the policy (Block authentication flows (opens in a new tab)). Reading sign-in logs: Reports Reader, Security Reader or Global Reader.
- Break-glass: have your emergency access accounts ready to exclude (Break-glass accounts).
Step by step
- Find current use. In Entra ID > Monitoring & health > Sign-in logs, add the Authentication protocol filter and choose device code (Sign-in logs section (opens in a new tab)). Check both interactive and non-interactive tabs. Note which users, apps and devices show up; legitimate uses are usually shared or input-limited devices such as conference room or signage devices.
- Check device registration. Since early September 2024, authentication flows policies that target All resources are also enforced on the Device Registration Service. If you use device code flow to register devices, filter the sign-in logs by the Device Registration Service in the Resource ID filter plus the Device code authentication protocol, and exclude that resource from the policy if needed (Enforcement on Device Registration Service (opens in a new tab)).
- Create the policy. Entra ID > Conditional Access > Policies > New policy (Device code flow policies (opens in a new tab)):
- Users: Include All users; Exclude your emergency access accounts and any documented exceptions.
- Target resources: All resources (formerly 'All cloud apps').
- Conditions > Authentication Flows: Configure = Yes, select Device code flow, Done.
- Grant: Block access.
- Enable policy: Report-only, then Create.
- Review report-only results for a week or two, then switch the policy to On (Block authentication flows (opens in a new tab)).
- Scope exceptions narrowly. If a device genuinely needs device code flow, Learn's example is to block it everywhere except a specific device platform in a specific network location (Device code flow (opens in a new tab)), rather than excluding whole user groups.
- Optional: authentication transfer. The same condition can block Authentication transfer (for example the Outlook desktop QR code that moves a session to a phone) for groups that shouldn't use Outlook on personal devices (Authentication transfer policies (opens in a new tab)).
Read-only PowerShell: which policies already control authentication flows
Read-only. Module: Microsoft.Graph.Identity.SignIns. Scope: Policy.Read.All. Roles supported for this read include Security Reader, Global Reader and Conditional Access Administrator (List policies (opens in a new tab)). Not tested against a live tenant.
Connect-MgGraph -Scopes "Policy.Read.All"
Get-MgIdentityConditionalAccessPolicy -All |
Where-Object { $_.Conditions.AuthenticationFlows.TransferMethods } |
ForEach-Object {
[pscustomobject]@{
Policy = $_.DisplayName
State = $_.State
TransferMethods = $_.Conditions.AuthenticationFlows.TransferMethods
IncludeApps = ($_.Conditions.Applications.IncludeApplications -join '; ')
ExcludeApps = ($_.Conditions.Applications.ExcludeApplications -join '; ')
ExcludeUsers = ($_.Conditions.Users.ExcludeUsers -join '; ')
ExcludeGroups = ($_.Conditions.Users.ExcludeGroups -join '; ')
Grant = ($_.GrantControls.BuiltInControls -join '; ')
}
} | Export-Csv .\ca-authentication-flows.csv -NoTypeInformation
transferMethods can be none, deviceCodeFlow, authenticationTransfer or unknownFutureValue (conditionalAccessAuthenticationFlows (opens in a new tab)). The v1.0 signIn resource doesn't expose the authentication protocol, so use the portal filter in step 1 to find device code sign-ins.
Gotchas
- Protocol tracking. Once a session used device code flow (or authentication transfer), it's "protocol tracked", and that state survives token refreshes. A later sign-in to a different app in the same session can be blocked even though it didn't use device code flow (Protocol tracking (opens in a new tab)).
- AADSTS530036 with "The refresh token is invalid due to authentication flow checks by Conditional Access" points to a protocol-tracked session under a policy that applies to all applications. Check Original transfer method in the sign-in's Basic info (Troubleshooting unexpected blocks (opens in a new tab)). Learn says there's no recommended remediation while the policy is enabled; after setting it to disabled or report-only, the device may need a fresh token.
- Admin tooling may use device code. Some on-premises agents offer a device code sign-in option during registration; for example, Microsoft Entra Password Protection proxy registration has an
-AuthenticateUsingDeviceCodemode (Deploy Password Protection (opens in a new tab)). Use the interactive mode instead once the block is on. - Service principals aren't covered by user-scoped policies (User exclusions (opens in a new tab)).
Check it worked
- In report-only, the sign-in's report-only results show Report-only: Failure for device code sign-ins the policy would block, and Report-only: Not applied for sign-ins that don't match its conditions (Policy evaluation results (opens in a new tab)).
- After enforcement, a test device code sign-in is blocked and the sign-in log names the authentication flows policy; selecting it shows which flow matched (Troubleshooting unexpected blocks (opens in a new tab)).
Roll back
Set the policy to Report-only or Off. Devices that were blocked because of protocol tracking may need to sign in again to get a fresh token.
Related on EntraReady: Conditional Access baseline, Block legacy authentication, Investigate a sign-in with the sign-in logs, Test a policy in report-only first.
← All GuidesIs this still accurate?
Spotted something outdated or wrong on this page? Tell us in a sentence. The page address is sent with it.