In this Video, I am going to discuss how to consume Web API Services with Basic Authentication. Please watch our previous Video before proceeding to this Video, where we discussed how to implement the Role-Based Basic Authentication in Web API with an example. We are also going to work with our previous example. As part of this Video, we are going to discuss the following pointers.
How to implement Web API Service with Basic Authentication?. The Complete ASP.NET Web API Developer Course 2022 [Videos].
- How to enable CORS in Web API?
- How to call Web API service from JQuery using Ajax?
- How to call Web API service from C# using a Console Application?
How to Enable CORS in Web API?
As we are going to consume the Web API Service using Jquery Ajax from another domain, we need to enable CORS in our application. Enabling CORS is a two steps process.
Step1: Install Microsoft.AspNet.WebApi.Cors package. Execute the following command using the NuGet Package Manager Console.
Step2: Include the following 2 lines of code in Register() method of WebApiConfig class in WebApiConfig.cs file which is inside App_Start folder
EnableCorsAttribute cors = new EnableCorsAttribute(“*”, “*”, “*”);
With the above two lines of code in place, the WebApiConfig class should look as below.
If you are new to CORS, then I strongly recommended you to watch the following Videos, where I discussed CORS in details.
Modifying the Controller:
In the controller, we need to create one method called GetEmployees. Depending on the credentials provided the Web API service should authenticate and return the correct results as follows.
If the AdminUser username and password are provided then only the male employees are returned from the service. Similarly, if SuperadminUser username and password are provided then only the female employees are returned from the service. In the same way, if both user username and password are provided then all the employees should be returned from the service else any other case it should return BAD Request from the service
To achieve this, lets modify the Employee Controller as shown below.
In the last Video, we discussed how to consume the Web APIs using Postman as a client. But it is also necessary to know how to consume APIs from different types of clients.
Consuming Web API Service using JQuery AJAX:
Create one empty web application and then add one HTML page with the name AJAXClient to your application. Once you add the HTML Page then copy and paste the following code.
Note: You need to be installed JQuery packages in your projects.
First, run the service application and then the client application. Now go to the client application and navigate to the URL /AJAXClient.html. Provide the valid username and password and click on the GetEmployees button and you should see the data as expected.
Now provide the wrong username and password and see the results as 401 unauthorized as expected.
Consuming Web API Service using C# Console Application:
Create a console application and then copy and paste the following code. You need to Install Newtonsoft.Json Package into your application
Here, we have given the AdminUser username and password, so it displays only the male employees as expected as shown in the below image.
In the next Video, I am going to discuss Message Handler in ASP.NET Web API with Examples. Here, in this Video, I try to explain how to Consuming Web API Service with Basic Authentication from JQuery AJAX as well as from a C# client.