-
Notifications
You must be signed in to change notification settings - Fork 345
Device Code Flow
This article is about MSAL.NET 3.x. If you are interested in MSAL.NET 2.x please to Device Code Flow in MSAL 2.x
Interactive authentication with Azure AD requires a web browser (for details see Usage of web browsers). However, in the case of devices and operating systems that do not provide a Web browser, Device code flow lets the user use another device (for instance another computer or a mobile phone) to sign-in interactively. By using the device code flow, the application obtains tokens through a two-step process especially designed for these devices/OS. Examples of such applications are applications running on iOT, or Command-Line tools (CLI). The idea is that:
-
Whenever user authentication is required, the app provides a code and asks the user to use another device (such as an internet-connected smartphone) to navigate to a URL (for instance, http://microsoft.com/devicelogin), where the user will be prompted to enter the code. That done, the web page will lead the user through a normal authentication experience, including consent prompts and multi-factor authentication if necessary.
-
Upon successful authentication, the command-line app will receive the required tokens through a back channel and will use it to perform the web API calls it needs.
-
MSAL.NET 2.2.0 and above
-
Device Code Flow is only available on public client applications
-
The authority passed in the
PublicClientApplicationBuilder
needs to be:- tenanted (of the form
https://login.microsoftonline.com/{tenant}/
wheretenant
is either the guid representing the tenant ID or a domain associated with the tenant. - or any work and school accounts (
https://login.microsoftonline.com/organizations/
)
Microsoft personal accounts are not yet supported by the Azure AD v2.0 endpoint (you cannot use /common or /consumers tenants)
- tenanted (of the form
IPublicClientApplication
contains a method called
AcquireTokenWithDeviceCode(IEnumerable<string> scopes, Func<DeviceCodeResult, Task> deviceCodeResultCallback)
This method takes as parameters:
- The
scopes
to request an access token for - A callback that will receive the
DeviceCodeResult
You can pass optional parameters, by calling:
-
.WithExtraQueryParameters(Dictionary{string, string})
to pass additional query parameters. This can be useful to target test environments, or for globalization (see below). You can passstring.Empty
. -
.WithAuthority(string, bool)
in order to override the default authority set at the application construction. Note that the overriding authority needs to be part of the known authorities added to the application construction.
The following sample code presents the most current case, with explanations of the kind of exceptions you can get, and their mitigations.
static async Task<AuthenticationResult> GetATokenForGraph()
{
string authority = "https://login.microsoftonline.com/contoso.com";
string[] scopes = new string[] { "user.read" };
IPublicClientApplication pca = PublicClientApplicationBuilder
.Create(clientId)
.WithAuthority(authority)
.Build();
AuthenticationResult result = null;
var accounts = await app.GetAccountsAsync();
// All AcquireToken* methods store the tokens in the cache, so check the cache first
try
{
result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilent.
// This indicates you need to call AcquireTokenInteractive to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
}
try
{
result = await app.AcquireTokenWithDeviceCode(scopes,
deviceCodeCallback =>
{
// This will print the message on the console which tells the user where to go sign-in using
// a separate browser and the code to enter once they sign in.
// The AcquireTokenWithDeviceCode() method will poll the server after firing this
// device code callback to look for the successful login of the user via that browser.
// This background polling (whose interval and timeout data is also provided as fields in the
// deviceCodeCallback class) will occur until:
// * The user has successfully logged in via browser and entered the proper code
// * The timeout specified by the server for the lifetime of this code (typically ~15 minutes) has been reached
// * The developing application calls the Cancel() method on a CancellationToken sent into the method.
// If this occurs, an OperationCanceledException will be thrown (see catch below for more details).
Console.WriteLine(deviceCodeResult.Message);
return Task.FromResult(0);
}).ExecuteAsync();
Console.WriteLine(result.Account.Username);
return result;
}
catch (MsalServiceException ex)
{
// Kind of errors you could have (in ex.Message)
// AADSTS50059: No tenant-identifying information found in either the request or implied by any provided credentials.
// Mitigation: as explained in the message from Azure AD, the authoriy needs to be tenanted. you have probably created
// your public client application with the following authorities:
// https://login.microsoftonline.com/common or https://login.microsoftonline.com/organizations
// AADSTS90133: Device Code flow is not supported under /common or /consumers endpoint.
// Mitigation: as explained in the message from Azure AD, the authority needs to be tenanted
// AADSTS90002: Tenant <tenantId or domain you used in the authority> not found. This may happen if there are
// no active subscriptions for the tenant. Check with your subscription administrator.
// Mitigation: if you have an active subscription for the tenant this might be that you have a typo in the
// tenantId (GUID) or tenant domain name.
}
catch (OperationCanceledException ex)
{
// If you use a CancellationToken, and call the Cancel() method on it, then this may be triggered
// to indicate that the operation was cancelled.
// See https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads
// for more detailed information on how C# supports cancellation in managed threads.
}
catch (MsalClientException ex)
{
// Verification code expired before contacting the server
// This exception will occur if the user does not manage to sign-in before a time out (15 mins) and the
// call to `AcquireTokenWithDeviceCodeAsync` is not cancelled in between
}
}
Sample | Platform | Description |
---|---|---|
active-directory-dotnetcore-devicecodeflow-v2 | Console (.NET Core) | .NET Core 2.1 console application letting a user acquire, with the Azure AD v2.0 endpoint, a token for the Microsoft Graph by singing in through another device having a Web browser |
In case you want to learn more about Device code flow:
- OAuth standard - device flow
- How this was done with the V1 endpoint: Device code flow in ADAL.NET
- Home
- Why use MSAL.NET
- Is MSAL.NET right for me
- Scenarios
- Register your app with AAD
- Client applications
- Acquiring tokens
- MSAL samples
- Known Issues
- AcquireTokenInteractive
- WAM - the Windows broker
- .NET Core
- Maui Docs
- Custom Browser
- Applying an AAD B2C policy
- Integrated Windows Authentication for domain or AAD joined machines
- Username / Password
- Device Code Flow for devices without a Web browser
- ADFS support
- Acquiring a token for the app
- Acquiring a token on behalf of a user in Web APIs
- Acquiring a token by authorization code in Web Apps
- High Availability
- Token cache serialization
- Logging
- Exceptions in MSAL
- Provide your own Httpclient and proxy
- Extensibility Points
- Clearing the cache
- Client Credentials Multi-Tenant guidance
- Performance perspectives
- Differences between ADAL.NET and MSAL.NET Apps
- PowerShell support
- Testing apps that use MSAL
- Experimental Features
- Proof of Possession (PoP) tokens
- Using in Azure functions
- Extract info from WWW-Authenticate headers
- SPA Authorization Code