Test your protected API

This tutorial is the final part of a series that demonstrates building and testing a protected web API that is registered in an external tenant. In Part 1 of this series, you created an ASP.NET Core web API and protected its endpoints. In this final step, you'll register the daemon app, and test your API.

In this tutorial, you learn how to:

  • Test a protected web API using a lightweight daemon app that calls the web API

Prerequisites

Tutorial: Secure an ASP.NET Core web API registered in an external tenant

Register the daemon app

The following steps show you how to register your daemon app in the Microsoft Entra admin center:

  1. Sign in to the Microsoft Entra admin center as at least an Application Developer.

  2. 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.

  3. Browse to Identity > Applications > App registrations.

  4. Select + New registration.

  5. In the Register an application page that appears, enter your application's registration information:

    1. In the Name section, enter a meaningful application name that will be displayed to users of the app, for example ciam-client-app.

    2. Under Supported account types, select Accounts in this organizational directory only.

  6. Select Register.

  7. 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.

  1. From the App registrations page, select the application that you created (such as ciam-client-app) to open its Overview page.
  2. Under Manage, select Certificates & secrets.
  3. Select New client secret.
  4. In the Description box, enter a description for the client secret (for example, ciam app client secret).
  5. Under Expires, select a duration for which the secret is valid (per your organizations security rules), and then select Add.
  6. Record the secret's Value. You'll 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.

  1. From the App registrations page, select the application that you created, such as ciam-client-app.

  2. Under Manage, select API permissions.

  3. Under Configured permissions, select Add a permission.

  4. Select the APIs my organization uses tab.

  5. In the list of APIs, select the API such as ciam-ToDoList-api.

  6. Select Application permissions option. We select this option as the app signs in as itself, but not on behalf of a user.

  7. From the permissions list, select TodoList.Read.All, ToDoList.ReadWrite.All (use the search box if necessary).

  8. Select the Add permissions button.

  9. At this point, you've assigned the permissions correctly. However, since the daemon app doesn't allow users to interact with it, the 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:

    1. Select Grant admin consent for <your tenant name>, then select Yes.
    2. Select Refresh, then verify that Granted for <your tenant name> appears under Status for both permissions.

Write code

  1. Initialize a .NET console app and navigate to its root folder

    dotnet new console -o MyTestApp
    cd MyTestApp
    
  2. Install MSAL to help you with handling authentication by running the following command:

    dotnet add package Microsoft.Identity.Client
    
  3. Run your API project and note the port on which it's running.

  4. 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("https://localhost:<your-api-port>/api/todolist");
    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.

  5. Remove the code in step 4 and replace with the following to test your API by sending a request with a valid access token.

    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(scopes).ExecuteAsync();
    
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    var response = await client.GetAsync("https://localhost:44351/api/todolist");
    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 with a valid access token. You should see the string: Your response is: OK printed in your console.

See also

Enable self-service password reset