Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you use a sample daemon application to acquire an access token and call a protected web API by using the Microsoft Authentication Library (MSAL).
A workforce tenant configuration is for your employees, internal apps, and other organizational resources.
The sample app you use in this quickstart acquires an access token to call Microsoft Graph API.
- An Azure account with an active subscription. If you don't already have one, Create an account.
- This Azure account must have permissions to manage applications. Any of the following Microsoft Entra roles include the required permissions:
- Application Administrator
- Application Developer
- Cloud Application Administrator
- A workforce tenant. You can use your Default Directory or set up a new tenant.
- Register a new app in the Microsoft Entra admin center, configured for Accounts in this organizational directory only. Refer to Register an application for more details. Record the following values from the application Overview page for later use:
- Application (client) ID
- Directory (tenant) ID
- Add a client secret to your app registration. Do not use client secrets in production apps. Use certificates or federated credentials instead. For more information, see add credentials to your application.
- A minimum requirement of .NET 6.0 SDK.
- Visual Studio 2022 or Visual Studio Code.
For daemon app to access data in Microsoft Graph API, you grant it the permissions it needs. The daemon app needs application type permissions. Users can't interact with a daemon application, so the tenant administrator must consent to these permissions. Use the following steps to grant and consent to the permissions:
For the .NET daemon app, you don't need to grant and consent to any permission. This daemon app reads its own app registration information, so it can do so without being granted any application permissions.
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.
To use your app registration details in the client daemon app sample, use the following steps:
Open a console window then navigate to the ms-identity-docs-code-dotnet/console-daemon directory:
cd ms-identity-docs-code-dotnet/console-daemon
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_obtained_from_the_Microsoft_Entra_admin_center 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 you recorded earlier from the overview page of the registered application.
You've configured your sample app. You can proceed to run and test it.
From your console window, run the following command to build and run the application:
dotnet run
Once the application runs successfully, it displays a response similar to the following snippet (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,
...
}
A daemon application acquires a token on behalf of itself (not on behalf of a user). Users can't interact with a daemon application because it requires its own identity. This type of application requests an access token by using its application identity by presenting its application ID, credential (secret or certificate), and an application ID URI. The daemon application uses the standard OAuth 2.0 client credentials grant flow to acquire an access token.
The app acquires an access token from Microsoft identity platform. The access token is scoped for the Microsoft Graph API. The app then uses the access token to request its own application registration details from Microsoft Graph API. The app can request any resource from Microsoft Graph API as long as the access token has the right permissions.
The sample demonstrates how an unattended job or Windows service can run with an application identity, instead of a user's identity.
Learn by building this ASP.NET web app with the series Tutorial: Register an application with the Microsoft identity platform.