C#.NET - Access Authorized REST Web API Method

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.

C#.NET - Access Authorized REST Web API Method

 
Prerequisites
 
Following are some prerequisites before you proceed any further in this tutorial:
  1. Understanding of JSON Object Mapper.
  2. Knowledge of REST Web API.
  3. Knowledge of ASP.NET MVC5.
  4. 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.
  1. ...  
  2.         public static async Task<string> GetInfo()  
  3.         {  
  4.             // Initialization.  
  5.             string responseObj = string.Empty;  
  6.   
  7.             // Posting.  
  8.             using (var client = new HttpClient())  
  9.             {  
  10.                 // Initialization  
  11.                 string userPassword = "AuthnticatedApiUser:PasswordForApi";  
  12.                 string authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes(userPassword));  
  13.   
  14.                 // Setting Authorization.  
  15.                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);  
  16.   
  17.                 // Setting Base address.  
  18. ...  
  19.                 // Setting Header key.  
  20.                     client.DefaultRequestHeaders.Add("X-ApiKey""MyRandomApiKeyValue");  
  21. ...  
  22.                 // Process HTTP GET/POST REST Web API  
  23. ...  
  24.                 // Reading Response.  
  25.                 string result = response.Content.ReadAsStringAsync().Result;  
  26.                 responseObj = result;  
  27.             }  
  28.   
  29.             return responseObj;  
  30.         }  
  31. ... 
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.:
  1. ...  
  2.      // Call REST Web API without parameters.  
  3.      string responseObj = Program.GetInfo().Result;  
  4. ... 
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.:
 

C#.NET - Access Authorized REST Web API Method

 
If the authorization of the REST Web API is not valid then the following will be received in the response i.e.:
 
C#.NET - Access Authorized REST Web API Method
 

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.


Similar Articles