| topic | sample | |||
|---|---|---|---|---|
| languages |
|
|||
| products |
|
|||
| description | Shows how a daemon console app uses MSAL.NET to get an access token and call Microsoft Graph. |
This sample application shows how to use the Microsoft identity platform to access the data of Microsoft business customers in Microsoft Graph in a long-running, non-interactive process. It uses the OAuth 2 client credentials grant to acquire an Access Tokens, which is then used to call the Microsoft Graph API and access organizational data.
The app is a .NET Core Console application. It gets the list of users in a Microsoft Entra tenant by using the Microsoft Authentication Library for .NET (MSAL.NET) to authenticate and acquire a token.
The console application:
- gets a token from Microsoft Entra ID for itself (without a user)
- and then calls the Microsoft Graph
/usersendpoint to get the list of users, which it then displays on the screen
For more information on the concepts used in this sample, be sure to read the Scenario: Daemon application that calls web APIs.
- Developers who wish to gain good familiarity of programming with Microsoft Graph are advised to go through the An introduction to Microsoft Graph for developers recorded session.
Daemon applications can use two forms of credentials to authenticate themselves with Microsoft Entra ID:
- Client secrets (also called application password).
- Certificates.
The first type (Client secret) is covered first the next paragraphs. A variation of this sample that uses a certificate, is also discussed at the end of this article in Variation: daemon application using client credentials with certificates
To run this sample, you'll need:
- Visual Studio and the .NET Core SDK
- An Internet connection
- A Windows machine (necessary if you want to run the app on Windows)
- An OS X machine (necessary if you want to run the app on Mac)
- A Linux machine (necessary if you want to run the app on Linux)
- a Microsoft Entra tenant. For more information on how to get a Microsoft Entra tenant, see How to get a Microsoft Entra tenant
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2.gitor download and extract the repository .zip file.
Given that the name of the sample is pretty long, and so are the name of the referenced NuGet packages, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
Navigate to the "1-Call-MSGraph" folder
cd "1-Call-MSGraph"There is one project in this sample. To register it, you can:
- either follow the steps Step 2: Register the sample with your Microsoft Entra tenant and Step 3: Configure the sample to use your Microsoft Entra tenant
- or use PowerShell scripts that:
- automatically creates the Microsoft Entra applications and related objects (passwords, permissions, dependencies) for you
- modify the Visual Studio projects' configuration files.
If you want to use this automation:
-
On Windows run PowerShell and navigate to the root of the cloned directory
-
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
-
Run the script to create your Microsoft Entra application and configure the code of the sample application accordingly.
cd AppCreationScripts .\Configure.ps1
Other ways of running the scripts are described in App Creation Scripts
-
Open the Visual Studio solution and click start
If you don't want to use this automation, follow the steps below
As a first step you'll need to:
- Sign in to the Microsoft Entra admin center using either a work or school account or a personal Microsoft account.
- If your account is present in more than one Microsoft Entra tenant, select
Directory + Subscriptionat the top right corner in the menu on top of the page, and switch your portal session to the desired Microsoft Entra tenant. - In the left-hand navigation pane, select the Microsoft Entra ID service, and then select App registrations.
-
Navigate to the Microsoft identity platform for developers App registrations page.
-
Select New registration.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
daemon-console. - In the Supported account types section, select Accounts in this organizational directory only ({tenant name}).
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
-
On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
-
From the Certificates & secrets page, in the Client secrets section, choose New client secret:
- Type a key description (for instance
app secret), - Select a key duration of either In 1 year, In 2 years, or Never Expires.
- When you press the Add button, the key value will be displayed, copy, and save the value in a safe location.
- You'll need this key later to configure the project in Visual Studio. This key value will not be displayed again, nor retrievable by any other means, so record it as soon as it is visible from the Microsoft Entra admin center.
- Type a key description (for instance
-
In the list of pages for the app, select API permissions
- Click the Add a permission button and then,
- Ensure that the Microsoft APIs tab is selected
- In the Commonly used Microsoft APIs section, click on Microsoft Graph
- In the Application permissions section, ensure that the right permissions are checked: User.Read.All
- Select the Add permissions button
-
At this stage permissions are assigned correctly but a daemon client app has no user interaction. Therefore no consent can be presented via a UI when the application is running. So the consent will need to be provided in the portal itself. Click the Grant/revoke admin consent for {tenant} button, and then select Yes when you are asked if you want to grant consent for the requested permission. You need to be a Microsoft Entra tenant admin to do this.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
Open the solution in Visual Studio to configure the project
Note: if you used the setup scripts, the changes below will have been applied for you, with the exception of the national cloud specific steps.
- Open the
daemon-console\appsettings.jsonfile. - Find the app key
TenantIdand replace the existing value with your Microsoft Entra tenant name. - Find the app key
ClientIdand replace the existing value with the application ID (clientId) of thedaemon-consoleapplication copied from the Microsoft Entra admin center. - Find the app key
ClientSecretand replace the existing value with the key you saved during the creation of thedaemon-consoleapp, in the Microsoft Entra admin center.
- Change the instance to the correct Microsoft Entra ID endpoint. See this reference for a list of Microsoft Entra ID endpoints..
- open the 'daemon-console\Program.cs' file and change the graph endpoint on lines in which there is a "graph.microsoft.com" reference. See this reference for more info on which graph endpoint to use.
Clean the solution, rebuild the solution, and run it.
Start the application, it will display the users in the tenant.
The relevant code for this sample is in the Program.cs file, in the Main method. The steps are:
-
Configure your application
Important note: even if we are building a console application, since it is a daemon, and therefore a confidential client application, as it does not access the Web APIs on behalf of a user, but on its own (application).
// Get the Token acquirer factory instance. By default it reads the configuration in an appsettings.json file // if it exists in the project. TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance(); // Configure the authentication options, add the services you need (Microsoft Graph, token cache) IServiceCollection services = tokenAcquirerFactory.Services; services.Configure<MicrosoftIdentityApplicationOptions>( option => tokenAcquirerFactory.Configuration.GetSection("AzureAd").Bind(option)) .AddMicrosoftGraph() .AddInMemoryTokenCaches(); // For more token cache serialization options, see https://aka.ms/msal-net-token-cache-serialization // Resolve the dependency injection. var serviceProvider = tokenAcquirerFactory.Build();
Here is an example of configuration (appsettings.json file)
{ "AzureAd": { "Instance": "https://login.microsoftonline.com/", "TenantId": "yourdomain.onmicrosoft.com", "ClientId": "1b4649ec-1111-2222-9821-bf5efe85ffdb", "ClientCredentials": [ { "SourceType": "ClientSecret", "ClientSecret": "your client secret here" } ] } } -
Call the Microsoft Graph API using the Graph SDK
In that case calling "https://graph.microsoft.com/v1.0/users" with the access token as a bearer token. You get the GraphServiceClient as a required service, and use it to request the list of users in the tenant. Given you are writing a daemon application, use
.WithAppOnly()to get an app-only token.try { GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>(); var users = await graphServiceClient.Users .GetAsync(r => r.Options.WithAppOnly()); } catch (ServiceException e) { Console.WriteLine("We could not retrieve the user's list: " + $"{e}"); }
Note that:
- You don't need to define the scopes here. Applications that authenticate as themselves, using client credentials, cannot specify, in the code, the individual scopes that they want to access. The scopes (app permissions) have to be statically declared during the application registration step. Therefore the only possible scope that can be specified in the code is "resource/.default" (here, "https://graph.microsoft.com/.default"). When you use the GraphServiceClient using
WithAppOnly, the scopes are automatically set to "https://graph.microsoft.com/.default" for you. - You don't need either to acquire a token. Microsoft.Identity.Web takes care of acquiring a token for you, and add it to the request made by Microsoft Graph
- You don't need to define the scopes here. Applications that authenticate as themselves, using client credentials, cannot specify, in the code, the individual scopes that they want to access. The scopes (app permissions) have to be statically declared during the application registration step. Therefore the only possible scope that can be specified in the code is "resource/.default" (here, "https://graph.microsoft.com/.default"). When you use the GraphServiceClient using
If you get an error Insufficient privileges to complete the operation. when calling the API, this is because the tenant administrator has not consent for the static permissions in the app registration portal. See step 6 of Register the client app (daemon-console) above.
You will typically see, on the output window, something like the following:
Failed to call the Web Api: Forbidden
Content: {
"error": {
"code": "Authorization_RequestDenied",
"message": "Insufficient privileges to complete the operation.",
"innerError": {
"request-id": "<a guid>",
"date": "<date>"
}
}
}| Exception | How to fix ? |
|---|---|
| IDW10503: Cannot determine the cloud Instance | Provide the configuration (appsettings.json with an "AzureAd" section, and "Instance" set) |
| System.ArgumentNullException: Value cannot be null. (Parameter 'tenantId') | Provide the TenantId in the configuration |
| Microsoft.Identity.Client.MsalClientException: No ClientId was specified. | Provide the ClientId in the configuration |
| ErrorCode: Client_Credentials_Required_In_Confidential_Client_Application | Provide a ClientCredentials section containing either a client secret, or a certificate or Pod identity if you run in AKS |
As we had explained earlier, daemon applications can use two types of credentials to authenticate themselves with Microsoft Entra ID. In the following section we will discuss how to use a certificate instead of a client secret.
To use certificates instead of an application secret you will need to do the following changes to what you have done so far:
- (optionally) generate a certificate and export it, i.e. if you don't have a certificate already.
- register the certificate with your application in the application registration portal
- enable the sample code to use certificates instead of the app secret.
If you want to use the automation script:
-
On Windows run PowerShell and navigate to the root of the cloned directory
-
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
-
Run the script to create your Microsoft Entra application and configure the code of the sample application accordingly.
.\AppCreationScripts-withCert\Configure.ps1Other ways of running the scripts are described in App Creation Scripts
If you don't want to use this automation, follow the following steps:
To complete this step, you will use the New-SelfSignedCertificate Powershell command.
-
Open PowerShell and run
New-SelfSignedCertificatewith the following parameters to create a self-signed certificate in the user certificate store on your computer:$cert=New-SelfSignedCertificate -Subject "CN=DaemonConsoleCert" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature
-
Export this certificate using the "Manage User Certificate" MMC snap-in accessible from the Windows Control Panel. You can also add other options to generate the certificate in a different store such as the Computer or service store (See How to: View Certificates with the MMC Snap-in).
Alternatively you can use an existing certificate if you have one (just be sure to record its name for the next steps)
In the application registration blade for your application, in the Certificates & secrets page, in the Certificates section:
- Click on Upload certificate and, in click the browse button on the right to select the certificate you just exported (or your existing certificate)
- Click Add
To change the visual studio project to enable certificates you need to:
- Open the
daemon-console\appsettings.jsonfile - Find the app key
Certificateand insert theCertificateDescriptionproperties of your certificate. You can see some examples below and read more about how to configure certificate descriptions here.
You can retrieve a certificate from your local store by adding the configuration below to the Certificate property in the daemon-console\appsettings.json file replacing <CERTIFICATE_STORE_PATH> with the store path to your certificate and <CERTIFICATE_DISTINGUISHED_NAME> with the distinguished name of your certificate. If you used the configuration scripts to generate the application this will be done for you using a sample self-signed certificate. You can read more about certificate stores here.
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "yourdomain.onmicrosoft.com",
"ClientId": "1b4649ec-1111-2222-9821-bf5efe85ffdb",
"ClientCredentials": [
{
"SourceType": "StoreWithDistinguishedName",
"CertificateStorePath": "<CERTIFICATE_STORE_PATH>",
"CertificateDistinguishedName": "<CERTIFICATE_DISTINGUISHED_NAME>"
}
]
}
}It's possible to get a certificate file, such as a pfx file, directly from a file path on your machine and load it into the application by using the configuration as shown below. Replace the values in the Certificate key of the daemon-console\appsettings.json file with the snippet shown below also replacing <PATH_TO_YOUR_CERTIFICATE_FILE> with the path to your certificate file and <PATH_TO_YOUR_CERTIFICATE_FILE> with that certificates password. If you created the application with the Configure.ps1 script found in the AppCreationScripts-withCert a pfx file called DaemonConsoleCert.pfx will be generated that is associated with certificate used as a credential for your app. If you like, you can use configure the Certificate property to reference this file and use it as a credential.
{
// ...
"ClientCredentials": [
{
"SourceType": "Path",
"CertificateDiskPath": "<PATH_TO_YOUR_CERTIFICATE_FILE>",
"CertificatePassword": "<CERTIFICATE_PASSWORD>"
}
]
}It's also possible to get certificates from an Azure Key Vault. Replace the values in the Certificate key of the daemon-console\appsettings.json file with the snippet shown below also replacing <YOUR_KEY_VAULT_URL> with the URL of the Key Vault holding your certificate and <YOUR_KEY_VAULT_CERTIFICATE_NAME> with the name of that certificate as shown in your Key Vault. If you created the application with the Configure.ps1 script found in the AppCreationScripts-withCert a pfx file called DaemonConsoleCert.pfx will be generated that is associated with certificate used as a credential for your app. If you like, you can load that certificate into a Key Vault and then access that Key Vault to use as a credential for your application. See the chapter 3 readme for more information.
{
// ...
"ClientCredentials": [
{
"SourceType": "KeyVault",
"KeyVaultUrl": "<YOUR_KEY_VAULT_URL>",
"KeyVaultCertificateName": "<YOUR_KEY_VAULT_CERTIFICATE_NAME>"
}
]
}Build and run your project. You have the same output, but this time, your application is authenticated with Microsoft Entra ID with the certificate instead of the application secret.
This application makes use of the Microsoft Identity Web Library to load the certificate based on the configurations in the daemon-console/appsettings.json for the ClientCredentials property settings.
Only the configuration changes. The rest of the application remains the same.
Learn how to:
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [msal dotnet].
If you find a bug in the sample, please raise the issue on GitHub Issues.
If you find a bug in msal.Net, please raise the issue on MSAL.NET GitHub Issues.
To provide a recommendation, visit the following User Voice page.
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 opencode@microsoft.com with any additional questions or comments.
For more information, see MSAL.NET's conceptual documentation:
- Quickstart: Register an application with the Microsoft identity platform
- Quickstart: Configure a client application to access web APIs
- Acquiring a token for an application with client credential flows
For more information about the underlying protocol:
For a more complex multi-tenant Web app daemon application, see active-directory-dotnet-daemon-v2

