Quickstart: .NET console app that accesses a protected web API
This quickstart uses a sample .NET console application to access a protected web API as its own identity by using the Microsoft Authentication Library (MSAL) for .NET. The application is a daemon application, which is a confidential client application, and uses the client credentials OAuth flow to get an access token to call the Microsoft Graph API.
Prerequisites
- An Azure account with an active subscription. If you don't already have one, Create an account.
- A minimum requirement of .NET 6.0 SDK
- Visual Studio 2022 or Visual Studio Code
Register the application and record identifiers
To complete registration, provide the application a name and specify the supported account types. Once registered, the application Overview pane displays the identifiers needed in the application source code.
Sign in to the Microsoft Entra admin center.
If you have access to multiple tenants, use the Settings icon in the top menu to switch to the tenant in which you want to register the application from the Directories + subscriptions menu.
Browse to Identity > Applications > App registrations, select New registration.
Enter a Name for the application, such as identity-client-daemon-app.
For Supported account types, select Accounts in this organizational directory only. For information on different account types, select the Help me choose option.
Select Register.
The application's Overview pane is displayed when registration is complete. Record the Directory (tenant) ID, the Application (client) ID and Object ID to be used in your application source code.
Note
The Supported account types can be changed by referring to Modify the accounts supported by an application.
Create a client secret
- In the Microsoft Entra admin center, browse to Identity > Applications > App registrations.
- Select the application that you registered earlier.
- In the application's Overview pane, select Certificates & secrets > New client secret.
- Enter a description for the secret in the Description field.
- Select an expiration period for the secret.
- Select Add.
- Record the Value of the client secret. The value is displayed only once, so make sure to record it.
Clone or download the sample application
To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.
To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:
git clone https://github.com/Azure-Samples/ms-identity-docs-code-dotnet.git
Download the .zip file. Extract it to a file path where the length of the name is fewer than 260 characters.
Configure the project
In your IDE, open the project folder, ms-identity-docs-code-dotnet/console-daemon, containing the sample.
Open Program.cs and replace the file contents with the following snippet;
// Full directory URL, in the form of https://login.partner.microsoftonline.cn/<tenant_id> Authority = " https://login.partner.microsoftonline.cn/Enter_the_tenant_ID_obtained_from_the_Microsoft_Entra_admin_center", // 'Enter the client ID obtained from the Microsoft Entra admin center ClientId = "Enter the client ID obtained from the Microsoft Entra admin center", // Client secret 'Value' (not its ID) from 'Client secrets' in the Microsoft Entra admin center ClientSecret = "Enter the client secret value obtained from the Microsoft Entra admin center", // Client 'Object ID' of app registration in Microsoft Entra admin center - this value is a GUID ClientObjectId = "Enter the client Object ID obtained from the Microsoft Entra admin center"
Authority
- The authority is a URL that indicates a directory that MSAL can request tokens from. Replace Enter_the_tenant_ID with the Directory (tenant) ID value that was recorded earlier.ClientId
- The identifier of the application, also referred to as the client. Replace the text in quotes with theApplication (client) ID
value that was recorded earlier from the overview page of the registered application.ClientSecret
- The client secret created for the application in the Microsoft Entra admin center. Enter the value of the client secret.ClientObjectId
- The object ID of the client application. Replace the text in quotes with theObject ID
value that was recorded earlier from the overview page of the registered application.
Run the application
In a terminal, navigate to the project directory, ms-identity-docs-code-dotnet/console-daemon.
Run the following command to build and run the application:
dotnet run
The application runs and displays a response similar to the following (shortened for brevity):
{ "@odata.context": "https://microsoftgraph.chinacloudapi.cn/v1.0/$metadata#applications/$entity", "id": "00001111-aaaa-2222-bbbb-3333cccc4444", "deletedDateTime": null, "appId": "00001111-aaaa-2222-bbbb-3333cccc4444", "applicationTemplateId": null, "disabledByMicrosoftStatus": null, "createdDateTime": "2021-01-17T15:30:55Z", "displayName": "identity-dotnet-console-app", "description": null, "groupMembershipClaims": null, ... }
Related content
- Learn by building this ASP.NET web app with the series Tutorial: Register an application with the Microsoft identity platform.
- Quickstart: Protect an ASP.NET Core web API with the Microsoft identity platform.
- Quickstart: Deploy an ASP.NET web app to Azure App Service