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.
Prerequisites
- 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 with the following configuration. For more information, see Register an application.
- Name: identity-client-daemon-app
- Supported account types: Accounts in this organizational directory only (Single tenant)
- A minimum requirement of .NET 6.0 SDK.
- Visual Studio 2022 or Visual Studio Code.
Create a client secret
Create a client secret for the registered application. The application uses the client secret to prove its identity when it requests for tokens:
- From the App registrations page, select the application that you created (such as web app client secret) to open its Overview page.
- Under Manage, select Certificates & secrets > Client secrets > New client secret.
- In the Description box, enter a description for the client secret (for example, web app client secret).
- Under Expires, select a duration for which the secret is valid (per your organizations security rules), and then select Add.
- Record the secret's Value. You use this value for configuration in a later step. The secret value won't be displayed again, and isn't retrievable by any means, after you navigate away from the Certificates and secrets. Make sure you record it.
When you create credentials for a confidential client application:
Microsoft recommends that you use a certificate instead of a client secret before moving the application to a production environment. For more information on how to use a certificate, see instructions in Microsoft identity platform application authentication certificate credentials.
For testing purposes, you can create a self-signed certificate and configure your apps to authenticate with it. However, in production, you should purchase a certificate signed by a well-known certificate authority, then use Azure Key Vault to manage certificate access and lifetime.
Grant API permissions to the daemon app
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.
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
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 Mifcrosoft 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.
Run and test the 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,
...
}
How it works
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.
Related content
Learn by building this ASP.NET web app with the series Tutorial: Register an application with the Microsoft identity platform.