AAD B2C- (3) Experience AAD B2C Protect WEB API

This article refers to the following Microsoft official documents, and all use ASP.NET as sample code, mainly to demonstrate the AAD B2C protection process of the API, to give you an intuitive feeling, in the subsequent content chapters, we will introduce how other languages ​​pass AAD B2C protects.

1. Use Azure Active Directory B2C to enable authentication in the web application


2. Use Azure Active Directory B2C to grant access to ASP.NET Web API


The case of the last lecture:

It mainly introduces a WEB APP website, developed using ASP.NET. The website uses AAD B2C for authentication and authorization. Users can directly call the AAD B2C user stream (that is, use the UI provided by AAD B2C) in the browser to complete user registration / login. / The basic function of changing the password, and at the same time, after the user logs in, the TOKEN information is returned to the WEB APP page.


The first case in this lecture:

It mainly introduces a new WEB API website, which is also developed using ASP.NET. The API of this website is protected by AAD B2C, and the WEB API and the WEB APP in the first case use the same AAD B2C tenant, that is to say After logging in, users in the first case (WEB APP) can directly use the Token after logging in to call the API of the second case (WEB API) to obtain data.

The second case in this lecture:

After experiencing the second case, we use Postman simulation with TOKEN to initiate a request to the WEB API to obtain data.

Video explanation:

You can watch this video at Station B: https://www.bilibili.com/video/BV17t4y1U7Um/



Graphic explanation:


The content of this section uses the code downloaded in the previous section, and continue to configure:

Create an application for the WEB API:

Note that the reply URL remains the same as the picture below;

The application ID URI can fill in a value by itself. Generally, this value identifies a set of API permissions.



In the WEB API to add the following two items published scope:

Under normal circumstances, we can use the increased scope to precisely control the access permissions of an API;

If there are multiple API permissions, multiple scopes can be configured here;

In this example, in order to cooperate with the sample code, we will set the read and write permissions. In the code, read means that the data can be obtained from the API, and the write flag can write data to the storage through the API.


Next, in the WEB APP application, authorize the scope of the WEB API, that is, the WEB APP can access the WEB API authority authorization:



Select backendapi and select all 3 scopes in the drop-down list:



Modify the WEB API configuration file:

In the video of this article, it introduces in detail where the configuration values ​​in the configuration file can be found at 06:56 seconds.



Modify the configuration file of WEB APP:

This video details how to configure these values ​​at 08:47 seconds;



Start the WEB API (Task Service) project:



After successful login, you can call the API in WEB API (TASK SERVICE) in TODO LIST and get the data:



You can see the content of TODO LIST as shown below:



We can observe the URL of the requested WEB API in the code:



Requesting the API in the previous step in Postman will result in authentication failure. We need to bring the token after the user logs in from the WEB APP to the request to access successfully:




Debug the code, copy and paste the Token value into Postman:


As you can see, the data was successfully fetched:



As of now, in the first three sections of AAD B2C, the user token is implemented through the UI. In subsequent chapters, we will share how to directly obtain the user Access Token through other methods and then call the WEB API.


This section is over!


Guess you like

Origin blog.51cto.com/10117438/2486635