Skip to content

Latest commit

 

History

History

console-cli

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
languages page_type name description products urlFragment
csharp
sample
.NET (C#) console application that makes a request to the Graph API via the Device Code flow
This .NET8 console application uses the device code flow for authentication and then makes a request to Microsoft Graph for the user's profile data.
azure
entra-id
ms-graph
ms-identity-docs-code-console-cli-csharp

.NET (C#) | console | user sign-in, protected web API access (Microsoft Graph) | Microsoft identity platform

This .NET 8 (C#) console application authenticates a user via the device code flow, and then makes a request to the Graph API as the authenticated user. The response to the request is printed to the console.

$ dotnet run
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXXX to authenticate.
{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
  "businessPhones": ["+1 (000) 1112222"],
  "displayName": "Contoso Employee",
  "givenName": "Contoso",
  "jobTitle": "Worker",
  "mail": "[email protected]",
  "mobilePhone": "1 000-111-2222",
  "officeLocation": "Contoso Plaza/F30",
  "preferredLanguage": null,
  "surname": "Employee",
  "userPrincipalName": "[email protected]",
  "id": "00aa11bb-cc22-dd33-ee44-ff55ee66dd77"
}

Prerequisites

  • Microsoft Entra tenant and the permissions or role required for managing app registrations in the tenant.
  • .NET 8.0 SDK

Setup

1. Register the app

First, complete the steps in Register an application with the Microsoft identity platform to register the application.

Use these settings in your app registration.

App registration
setting
Value for this sample app Notes
Name dotnet-device-code-flow-app Suggested value for this sample.
You can change the app name at any time.
Supported account types Accounts in this organizational directory only (Single tenant) Suggested value for this sample.
Platform type None No redirect URI required; don't select a platform.
Allow public client flows Yes Required value for this sample.

ℹ️ Bold text in the tables above matches (or is similar to) a UI element in the Microsoft Entra admin center, while code formatting indicates a value you enter into a text box in the Microsoft Entra admin center.

2. Update the Program.cs file with app registration values

// 'Directory (tenant) ID' of app registration in the Microsoft Entra admin center - this value is a GUID
TenantId = "",

// 'Application (client) ID' of app registration in Microsoft Entra admin center - this value is a GUID
ClientId = ""

Run the application

dotnet run

Follow the device code flow instructions that are presented. If everything worked, you should receive a response similar to this:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
  "businessPhones": ["+1 (000) 1112222"],
  "displayName": "Contoso Employee",
  "givenName": "Contoso",
  "jobTitle": "Worker",
  "mail": "[email protected]",
  "mobilePhone": "1 000-111-2222",
  "officeLocation": "Contoso Plaza/F30",
  "preferredLanguage": null,
  "surname": "Employee",
  "userPrincipalName": "[email protected]",
  "id": "00aa11bb-cc22-dd33-ee44-ff55ee66dd77"
}

About the code

This .NET 6 (C#) console application prompts the user to sign in via their device using a code provided by Microsoft Authentication Library (MSAL). The user completes this flow in their chosen web browser. Upon successful authentication, an HTTP GET request to the Microsoft Graph /me endpoint is issued with the user's access token in the HTTP header. The response from the GET request is then displayed in the console.

This sample does not demonstrate the usage of cached access tokens. Access token caching should be used in situations where the console application will need to access the same protected API using the same access token multiple times across the life of the user's session in the application. This sample, as written, does not perform multiple calls and wouldn't result in a token cache hit. For additional information, see Get a token from the token cache using MSAL.NET.

Reporting problems

Sample app not working?

If you can't get the sample working, you've checked Stack Overflow, and you've already searched the issues in this sample's repository, open an issue report the problem.

  1. Search the GitHub issues in the repository - your problem might already have been reported or have an answer.
  2. Nothing similar? Open an issue that clearly explains the problem you're having running the sample app.

All other issues

⚠️ WARNING: Any issue in this repository not limited to running one of its sample apps will be closed without being addressed.

For all other requests, see Support and help options for developers | Microsoft identity platform.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.