Test a protected ASP.NET Core web API
Applies to: Workforce tenants
External tenants (learn more)
This tutorial is the final part of a series that demonstrates building and testing a protected web API registered in a Microsoft Entra tenant. In Part 1 of this series, you created an ASP.NET Core web API and protected its endpoints. You'll now create a lightweight daemon app, register it in your tenant, and use the daemon app to test the web API you built.
In this tutorial, you learn how to:
- Register a daemon app
- Assign an app role to your daemon app
- Build your daemon app
- Run your daemon app to call the protected web API
Prerequisites
- If you haven't already, complete the Tutorial: Build and protect an ASP.NET Core web API with the Microsoft identity platform
Register the daemon app
The following steps show you how to register your daemon app in the Microsoft Entra admin center:
Sign in to the Microsoft Entra admin center as at least an Application Developer.
If you have access to multiple tenants, use the Settings icon
in the top menu to switch to your external tenant from the Directories + subscriptions menu.
Browse to Identity > Applications > App registrations.
Select + New registration.
In the Register an application page that appears, enter your application's registration information:
In the Name section, enter a meaningful application name that will be displayed to users of the app, for example ciam-client-app.
Under Supported account types, select Accounts in this organizational directory only.
Select Register.
The application's Overview pane is displayed when registration is complete. Record the Directory (tenant) ID and the Application (client) ID to be used in your application source code.
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.
Assign app role to your daemon app
Apps authenticating by themselves require app permissions.
From the App registrations page, select the daemon application that you created.
Under Manage, select API permissions.
Under Configured permissions, select Add a permission.
Select the APIs my organization uses tab.
In the list of APIs, select the web API you registered earlier.
Select Application permissions option. We select this option as the app signs in as itself, but not on behalf of a user.
From the permissions list, select Forecast.Read (use the search box if necessary).
Select the Add permissions button.
At this point, you've assigned the permissions correctly. However, since the daemon app doesn't allow users to interact with it, users themselves can't consent to these permissions. To address this problem, you as the admin must consent to these permissions on behalf of all the users in the tenant:
- Select Grant admin consent for <your tenant name>, then select Yes.
- Select Refresh, then verify that Granted for <your tenant name> appears under Status for the permissions.
Build a daemon app
Initialize a .NET console app and navigate to its root folder:
dotnet new console -o MyTestApp cd MyTestApp
Install MSAL.NET to help with handling authentication by running the following command:
dotnet add package Microsoft.Identity.Client
Run your API project and note the port on which it's running.
Open the Program.cs file and replace the "Hello world" code with the following code.
using System; using System.Net.Http; using System.Net.Http.Headers; HttpClient client = new HttpClient(); var response = await client.GetAsync("http://localhost:<your-api-port>/weatherforecast"); Console.WriteLine("Your response is: " + response.StatusCode);
Navigate to the daemon app root directory and run app using the command
dotnet run
. This code sends a request without an access token. You should see the string: Your response is: Unauthorized printed in your console.Remove the code in step 4 and replace with the following to test your API by sending a request with a valid access token. This daemon app uses the client credentials flow to acquire an access token as it authenticates without user interaction.
using Microsoft.Identity.Client; using System; using System.Net.Http; using System.Net.Http.Headers; HttpClient client = new HttpClient(); var clientId = "<your-daemon-app-client-id>"; var clientSecret = "<your-daemon-app-secret>"; var scopes = new[] {"api://<your-web-api-application-id>/.default"}; var tenantName= "<your-tenant-name>"; var authority = $"https://{tenantName}.ciamlogin.com/"; var app = ConfidentialClientApplicationBuilder .Create(clientId) .WithAuthority(authority) .WithClientSecret(clientSecret) .Build(); var result = await app.AcquireTokenForClient(new string[] { scopes }).ExecuteAsync(); Console.WriteLine($"Access Token: {result.AccessToken}"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); var response = await client.GetAsync("http://localhost:/<your-api-port>/weatherforecast"); var content = await response.Content.ReadAsStringAsync(); Console.WriteLine("Your response is: " + response.StatusCode); Console.WriteLine(content);
Navigate to the daemon app root directory and run app using the command
dotnet run
. This code sends a request with a valid access token. You should see the string: Your response is: OK printed in your console alongside some dummy weather forecast data from our minimal API.Your response is: OK [{"date":"2025-03-01","temperatureC":45,"summary":"Warm","temperatureF":112}, {"date":"2025-03-02","temperatureC":7,"summary":"Freezing","temperatureF":44}, {"date":"2025-03-03","temperatureC":48,"summary":"Sweltering","temperatureF":118}, {"date":"2025-03-04","temperatureC":-20,"summary":"Chilly","temperatureF":-3}, {"date":"2025-03-05","temperatureC":12,"summary":"Scorching","temperatureF":53}]