REST
Web APIs are used for exchanging data between client and server
machines in order to protect data from misuse. Another important
aspect of REST web APIs is authorization that adds more security layers for data exchange between client and server machines.
Today, I shall be demonstrating consumption of Authorized REST Web API methods using ASP.NET REST Web API platform.
Prerequisites
Following are some prerequisites before you proceed any further in this tutorial:
- Understanding of JSON Object Mapper.
- Knowledge of REST Web API.
- Knowledge of ASP.NET MVC5.
- Knowledge of C# Programming.
The example code is
being developed in Microsoft Visual Studio 2019 Professional. The sample
sales data is taken randomly from the internet. I have used ASP.NET MVC - REST Web API Basic Authorization using Nuget Library solution as server side.
Let's begin now.
Step 1
Create new C#.NET Console Application project and name it "AccessAuthorizeRESTWebApi".
Step 2
Create target JSON object mappers for request/response objects according to ASP.NET MVC - REST Web API Basic Authorization using Nuget Library server side solution if required.
Step 3
Install "Newtonsoft.Json" & "Microsoft.AspNet.WebApi.Client" NuGet libraries.
Step 4
Create "GetInfo" method without parameters in "Program.cs" file and replace the following code in it i.e.
- ...
- public static async Task<string> GetInfo()
- {
-
- string responseObj = string.Empty;
-
-
- using (var client = new HttpClient())
- {
-
- string userPassword = "AuthnticatedApiUser:PasswordForApi";
- string authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes(userPassword));
-
-
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
-
-
- ...
-
- client.DefaultRequestHeaders.Add("X-ApiKey", "MyRandomApiKeyValue");
- ...
-
- ...
-
- string result = response.Content.ReadAsStringAsync().Result;
- responseObj = result;
- }
-
- return responseObj;
- }
- ...
In the above code, I am using "HttpClient"
library to consume/access Authorized REST Web API method. First I have
initialized my username/password contract which is required to access
the REST Web API in correspondence to ASP.NET MVC - REST Web API Basic Authorization using Nuget Library
server side solution, then after I initialized my base URL, I have added the REST Web API security key to the content header. Know that security
key depends on your server side contract, it is not neccessary that your
server side have any security key at all. Finally, after
successfully receiving data from the server I have set my response
object.
Step 5
In "Program.cs" file "Main" method write the following line of code to call the GET type REST Web API method with and without request query parameters i.e.:
- ...
-
- string responseObj = Program.GetInfo().Result;
- ...
In the above lines of code, I am simply calling my Authorized REST web API method and mapping the response as string object.
Step 6
If you execute the provided solution, you will be able to see the following, but, you will need to execute the ASP.NET MVC - REST Web API Basic Authorization using Nuget Library
server side solution first i.e.:
If the authorization of the REST Web API is not valid then the following will be received in the response i.e.:
Conclusion
In this article, you will learn to consume Authorized
REST Web API method using ASP.NET REST Web API platform. You will also
learn to utilize "HttpClient" library to consume REST Web APIs. You will
learn to pass contracted username/password in your "HttpClient" library
and finally you will learn to add security key into the header of HTTP
request call.