Question

Difficulty: MediumMicrosoft Identity Platform Authentication

You are developing a C# background worker service that runs on an on-premises server. The service must periodically query a secured downstream web API without any user interaction. You register the service in Microsoft Entra ID as a daemon application. You need to write code using MSAL.NET to acquire an access token for the downstream API.

Which two code segments should you use to instantiate the application client and acquire the token? (Select two.)

  1. var app = ConfidentialClientApplicationBuilder.Create(clientId).WithClientSecret(clientSecret).WithAuthority(authority).Build();Answer
  2. B
    var app = PublicClientApplicationBuilder.Create(clientId).WithAuthority(authority).Build();
  3. var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();Answer
  4. D
    var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();

Answer

To instantiate the application client and acquire the token for a daemon application, you must use ConfidentialClientApplicationBuilder to create the client and call AcquireTokenForClient to retrieve the token.
For daemon applications running without user interaction, MSAL.NET requires a confidential client application configuration. The correct setup uses ConfidentialClientApplicationBuilder to define the client with a client secret and AcquireTokenForClient to perform the OAuth 2.0 Client Credentials grant flow.

Step-by-Step Solution

1
Determine the application type and flow based on the scenario requirements.
Since the background worker service runs on an on-premises server without user interaction, it represents a daemon application that must use the OAuth 2.0 Client Credentials flow.
Daemon applications run headlessly and must authenticate using their own identity (app identity) rather than a user's identity.
2
Select the correct MSAL.NET application builder to instantiate the client.
Use ConfidentialClientApplicationBuilder because it supports configuring confidential clients with credentials like client secrets or certificates.
PublicClientApplicationBuilder does not support client credentials and is meant for interactive client applications.
3
Select the correct method to request and acquire the access token.
Call the AcquireTokenForClient method on the instantiated confidential client application instance.
AcquireTokenForClient initiates the non-interactive Client Credentials flow, whereas AcquireTokenInteractive requires a UI browser session for user interaction.

Key Concept

Authenticating daemon applications with MSAL.NET using the Client Credentials flow
Rate this question