Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
- An Azure account with an active subscription. Create an account.
- Register a new app in the Microsoft Entra admin center and record its identifiers from the app Overview page. For more information, see Register an application.
- Name: NewWebAPI1
- Supported account types: Accounts in this organizational directory only (Single tenant)
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.
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:
From the App registrations page, select the API application that you created (ciam-ToDoList-api) to open its Overview page.
Under Manage, select Expose an API.
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.
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.Under Scopes defined by this API, select Add a scope.
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 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.
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:
From the App registrations page, select the application that you created (such as ciam-ToDoList-api) to open its Overview page.
Under Manage, select App roles.
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 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
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 code sample to match the registered web API.
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.
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 thevalue
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 thevalue
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. ReplaceEnter_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.
- For workforce tenants, use
Run the following command from the root of your web API project directory to start the app:
dotnet run
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.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.
Learn how to protect an ASP.NET Core web API with the Microsoft identity platform.