Quickstart: .NET console app that accesses a protected web API
Článok
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 for free.
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.
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.
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;
C#
// Full directory URL, in the form of https://login.microsoftonline.com/<tenant_id>
Authority = " https://login.microsoftonline.com/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 the Application (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 the Object 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:
Console
dotnet run
The application runs and displays a response similar to the following (shortened for brevity):