Quickstart: Call a web API that is protected by the Microsoft identity platform

In this quickstart, you use a sample web app to show you how to protect an ASP.NET web API by using the Microsoft identity platform. The sample uses the Microsoft Authentication Library (MSAL) to handle authentication and authorization.

Prerequisites

Expose the API

Once the API is registered, you can configure its permission by defining the scopes that the API exposes to client applications. Client applications request permission to perform operations by passing an access token along with its requests to the protected web API. The web API then performs the requested operation only if the access token it receives contains the required scopes.

Add delegated permissions (scopes)

An API needs to publish a minimum of one scope, also called Delegated Permission, for the client apps to obtain an access token for a user successfully. To publish a scope, follow these steps:

  1. From the App registrations page, select the API application that you created (ciam-ToDoList-api) to open its Overview page.

  2. Under Manage, select Expose an API.

  3. At the top of the page, next to Application ID URI, select the Add link to generate a URI that is unique for this app.

  4. Accept the proposed Application ID URI such as api://{clientId}, and select Save. When your web application requests an access token for the web API, it adds the URI as the prefix for each scope that you define for the API.

  5. Under Scopes defined by this API, select Add a scope.

  6. Enter the following values that define a read access to the API, then select Add scope to save your changes:

    Property Value
    Scope name ToDoList.Read
    Who can consent Admins only
    Admin consent display name Read users ToDo list using the 'TodoListApi'
    Admin consent description Allow the app to read the user's ToDo list using the 'TodoListApi'.
    State Enabled
  7. Select Add a scope again, and enter the following values that define a read and write access scope to the API. Select Add scope to save your changes:

    Property Value
    Scope name ToDoList.ReadWrite
    Who can consent Admins only
    Admin consent display name Read and write users ToDo list using the 'ToDoListApi'
    Admin consent description Allow the app to read and write the user's ToDo list using the 'ToDoListApi'
    State Enabled

Learn more about the principle of least privilege when publishing permissions for a web API.

Add application permissions (app roles)

An API needs to publish a minimum of one app role for applications, also called Application permission, for the client apps to obtain an access token as themselves. Application permissions are the type of permissions that APIs should publish when they want to enable client applications to successfully authenticate as themselves and not need to sign-in users. To publish an application permission, follow these steps:

  1. From the App registrations page, select the application that you created (such as ciam-ToDoList-api) to open its Overview page.

  2. Under Manage, select App roles.

  3. Select Create app role, then enter the following values, then select Apply to save your changes:

    Property Value
    Display name ToDoList.Read.All
    Allowed member types Applications
    Value ToDoList.Read.All
    Description Allow the app to read every user's ToDo list using the 'TodoListApi'
    Do you want to enable this app role? Keep it checked
  4. Select Create app role again, then enter the following values for the second app role, then select Apply to save your changes:

    Property Value
    Display name ToDoList.ReadWrite.All
    Allowed member types Applications
    Value ToDoList.ReadWrite.All
    Description Allow the app to read and write every user's ToDo list using the 'ToDoListApi'
    Do you want to enable this app role? Keep it checked

Screenshot that shows the field values when adding the scope to an API.

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.

git clone https://github.com/Azure-Samples/ms-identity-ciam-dotnet-tutorial.git
  • Download the .zip file. Extract it to a file path where the length of the name is fewer than 260 characters.

Configure the sample application

Configure the code sample to match the registered web API.

  1. In your IDE, open the project folder, ms-identity-ciam-dotnet-tutorial/2-Authorization/3-call-own-api-dotnet-core-daemon/ToDoListAPI, containing the sample.

  2. Open appsettings.json file, which contains the following code snippet:

    {
      "AzureAd": {
        "Instance": "Enter_the_Authority_URL_Here", 
        "TenantId": "Enter_the_Tenant_Id_Here",
        "ClientId": "Enter_the_Application_Id_Here",
        "Scopes": {
          "Read": ["ToDoList.Read", "ToDoList.ReadWrite"],
          "Write": ["ToDoList.ReadWrite"]
        },
        "AppPermissions": {
          "Read": ["ToDoList.Read.All", "ToDoList.ReadWrite.All"],
          "Write": ["ToDoList.ReadWrite.All"]
        }
      },
      "Logging": {...},
      "AllowedHosts": "*"
    }
    

    Find the following values:

    • ClientId - The identifier of the application, also referred to as the client. Replace the value text in quotes with Application (client) ID that was recorded earlier from the Overview page of the registered application.
    • TenantId - The identifier of the tenant where the application is registered. Replace the value text in quotes with Directory (tenant) ID value that was recorded earlier from the Overview page of the registered application.
    • Instance - It specifies the directory from which the Microsoft Authentication Library (MSAL) can request tokens from. Replace Enter_the_Authority_URL_Here with either of the following values depending on your scenario:
      • For workforce tenants, use https://login.partner.microsoftonline.cn/ as the instance.

Run the sample application

  1. Run the following command from the root of your web API project directory to start the app:

    dotnet run
    
  2. If everything worked correctly, your terminal displays an output similar to the following:

     Building...
         info: Microsoft.Hosting.Lifetime[14]
               Now listening on: https://localhost:{port}
         info: Microsoft.Hosting.Lifetime[0]
               Application started. Press Ctrl+C to shut down.
         info: Microsoft.Hosting.Lifetime[0]
               Hosting environment: Development
    ...
    

    Record the port number in the https://localhost:{port} URL.

  3. To verify the endpoint is protected, update the base URL in the following cURL command to match the one you received in the previous step, and then run the command:

    curl -k -X GET https://localhost:<your-api-port>/api/todolist -w "%{http_code}\n"
    

    The expected response is 401 Unauthorized.

Next steps

Learn how to protect an ASP.NET Core web API with the Microsoft identity platform.