|
| 1 | +--- |
| 2 | +title: Call an API in a sample .NET daemon application |
| 3 | +description: Learn how to configure a sample .NET daemon application that calls an API protected with Azure Active Directory (Azure AD) for customers |
| 4 | +services: active-directory |
| 5 | +author: SHERMANOUKO |
| 6 | +manager: mwongerapk |
| 7 | + |
| 8 | +ms.author: shermanouko |
| 9 | +ms.service: active-directory |
| 10 | +ms.subservice: ciam |
| 11 | +ms.topic: sample |
| 12 | +ms.date: 07/13/2023 |
| 13 | + |
| 14 | +#Customer intent: As a dev, devops, I want to configure a sample .NET daemon application that calls an API protected by Azure Active Directory (Azure AD) for customers tenant |
| 15 | +--- |
| 16 | + |
| 17 | +# Call an API in a sample .NET daemon application |
| 18 | + |
| 19 | +This article uses a sample .NET daemon application to show you how a daemon application acquires a token to call a protected web API. Azure Active Directory (Azure AD) for customers protects the Web API. |
| 20 | + |
| 21 | +A daemon application acquires a token on behalf of itself (not on behalf of a user). Users can't interact with a daemon application because it requires its own identity. This type of application requests an access token by using its application identity and presenting its application ID, credential (password or certificate), and application ID URI to Azure AD. |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- [.NET 7.0](https://dotnet.microsoft.com/download/dotnet/7.0) or later. |
| 26 | + |
| 27 | +- [Visual Studio Code](https://code.visualstudio.com/download) or another code editor. |
| 28 | + |
| 29 | +- Azure AD for customers tenant. If you don't already have one, [sign up for a free trial](https://aka.ms/ciam-free-trial?wt.mc_id=ciamcustomertenantfreetrial_linkclick_content_cnl)</a>. |
| 30 | + |
| 31 | +## Register a daemon application and a web API |
| 32 | + |
| 33 | +In this step, you create the daemon and the web API application registrations, and you specify the scopes of your web API. |
| 34 | + |
| 35 | +### Register a web API application |
| 36 | + |
| 37 | +[!INCLUDE [active-directory-b2c-app-integration-add-user-flow](./includes/register-app/register-api-app.md)] |
| 38 | + |
| 39 | +### Configure application roles |
| 40 | + |
| 41 | +[!INCLUDE [active-directory-b2c-app-integration-add-user-flow](./includes/register-app/add-app-role.md)] |
| 42 | + |
| 43 | +### Configure optional claims |
| 44 | + |
| 45 | +[!INCLUDE [active-directory-b2c-app-integration-add-user-flow](./includes/register-app/add-optional-claims-access.md)] |
| 46 | + |
| 47 | +### Register the daemon application |
| 48 | + |
| 49 | +[!INCLUDE [active-directory-b2c-register-app](./includes/register-app/register-client-app-common.md)] |
| 50 | + |
| 51 | +### Create a client secret |
| 52 | + |
| 53 | +[!INCLUDE [active-directory-b2c-app-integration-add-user-flow](./includes/register-app/add-app-client-secret.md)] |
| 54 | + |
| 55 | +### Grant API permissions to the daemon application |
| 56 | + |
| 57 | +[!INCLUDE [active-directory-b2c-app-integration-add-user-flow](./includes/register-app/grant-api-permissions-app-permissions.md)] |
| 58 | + |
| 59 | +## Clone or download sample daemon application and web API |
| 60 | + |
| 61 | +To get the web application sample code, you can do either of the following tasks: |
| 62 | + |
| 63 | +- [Download the .zip file](https://github.com/Azure-Samples/ms-identity-ciam-dotnet-tutorial/archive/refs/heads/main.zip) or clone the sample web application from GitHub by running the following command: |
| 64 | + |
| 65 | + ```console |
| 66 | + git clone https://github.com/Azure-Samples/ms-identity-ciam-dotnet-tutorial.git |
| 67 | + ``` |
| 68 | +If you choose to download the *.zip* file, extract the sample application file to a folder where the total length of the path is 260 or fewer characters. |
| 69 | + |
| 70 | +## Configure the sample daemon application and API |
| 71 | + |
| 72 | +To use your app registration in the client web application sample: |
| 73 | + |
| 74 | +1. In your code editor, open *ms-identity-ciam-dotnet-tutorial/2-Authorization/3-call-own-api-dotnet-core-daemon/ToDoListClient/appsettings.json* file. |
| 75 | + |
| 76 | +1. Find the placeholder: |
| 77 | + |
| 78 | + - `Enter_the_Application_Id_Here` and replace it with the Application (client) ID of the daemon application you registered earlier. |
| 79 | + |
| 80 | + - `Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant name, learn how to [read your tenant details](how-to-create-customer-tenant-portal.md#get-the-customer-tenant-details). |
| 81 | + |
| 82 | + - `Enter_the_Client_Secret_Here` and replace it with the daemon application secret value you copied earlier. |
| 83 | + |
| 84 | + - `Enter_the_Web_Api_Application_Id_Here` and replace it with the Application (client) ID of the web API you copied earlier. |
| 85 | + |
| 86 | +To use your app registration in the web API sample: |
| 87 | + |
| 88 | +1. In your code editor, open *ms-identity-ciam-dotnet-tutorial/2-Authorization/3-call-own-api-dotnet-core-daemon/ToDoListAPI/appsettings.json* file. |
| 89 | + |
| 90 | +1. Find the placeholder: |
| 91 | + |
| 92 | + - `Enter_the_Application_Id_Here` and replace it with the Application (client) ID of the web API you copied. |
| 93 | + |
| 94 | + - `Enter_the_Tenant_Id_Here` and replace it with the Directory (tenant) ID you copied earlier. |
| 95 | + |
| 96 | + - `Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant name, learn how to [read your tenant details](how-to-create-customer-tenant-portal.md#get-the-customer-tenant-details). |
| 97 | + |
| 98 | +## Run and test sample daemon application and API |
| 99 | + |
| 100 | +1. Open a console window, then run the web API by using the following commands: |
| 101 | + |
| 102 | + ```console |
| 103 | + cd 2-Authorization\3-call-own-api-dotnet-core-daemon\ToDoListAPI |
| 104 | + dotnet run |
| 105 | + ``` |
| 106 | +1. Run the daemon client by using the following commands: |
| 107 | + |
| 108 | + ```console |
| 109 | + cd 2-Authorization\3-call-own-api-dotnet-core-daemon\ToDoListClient |
| 110 | + dotnet run |
| 111 | + ``` |
| 112 | + |
| 113 | +If your daemon application and web API successfully run, you should see something similar to the following JSON array in your console window |
| 114 | + |
| 115 | +```bash |
| 116 | +Posting a to-do... |
| 117 | +Retrieving to-do's from server... |
| 118 | +To-do data: |
| 119 | +ID: 1 |
| 120 | +User ID: 41b1e1a8-8e51-4514-8dab-e568afa2826c |
| 121 | +Message: Bake bread |
| 122 | +Posting a second to-do... |
| 123 | +Retrieving to-do's from server... |
| 124 | +To-do data: |
| 125 | +ID: 1 |
| 126 | +User ID: 41b1e1a8-8e51-4514-8dab-e568afa2826c |
| 127 | +Message: Bake bread |
| 128 | +ID: 2 |
| 129 | +User ID: 41b1e1a8-8e51-4514-8dab-e568afa2826c |
| 130 | +Message: Butter bread |
| 131 | +Deleting a to-do... |
| 132 | +Retrieving to-do's from server... |
| 133 | +To-do data: |
| 134 | +ID: 2 |
| 135 | +User ID: 41b1e1a8-8e51-4514-8dab-e568afa2826c |
| 136 | +Message: Butter bread |
| 137 | +Editing a to-do... |
| 138 | +Retrieving to-do's from server... |
| 139 | +To-do data: |
| 140 | +ID: 2 |
| 141 | +User ID: 41b1e1a8-8e51-4514-8dab-e568afa2826c |
| 142 | +Message: Eat bread |
| 143 | +Deleting remaining to-do... |
| 144 | +Retrieving to-do's from server... |
| 145 | +There are no to-do's in server |
| 146 | +``` |
| 147 | + |
| 148 | +## How it works |
| 149 | + |
| 150 | +The daemon application use [OAuth2.0 client credentials grant](../../develop/v2-oauth2-client-creds-grant-flow.md) to acquire an access token for itself and not for the user. The access token that the app requests contains the permissions represented as roles. The client credential flow uses this set of permissions in place of user scopes for application tokens. You [exposed these application permissions](#configure-application-roles) in the web API earlier, then [granted them to the daemon app](#grant-api-permissions-to-the-daemon-application). The daemon app in this article uses [Microsoft Authentication Library for .NET](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet) to simplify the process of acquiring a token. |
| 151 | + |
| 152 | +On the API side, the web API must verify that the access token has the required permissions (application permissions). The web API rejects access tokens that don't have the required permissions. |
| 153 | + |
| 154 | +## See also |
| 155 | + |
| 156 | +See the tutorial on how to [build your own .NET daemon app that calls an API](./tutorial-daemon-dotnet-call-api-prepare-tenant.md) |
0 commit comments