ASP.NET MVC - REST Web API Basic Authorization using Nuget Library

Introduction 
 
When a REST Web API is created to share data across multiple devices (e.g. mobile devices, desktop applications or any website), then the authorization of REST Web API becomes a vital aspect in order to protect data sensitivity from any outside breaches. I have recently created my own small REST Web API Authorization library Asmak9.AuthorizeRESTWebApiAK which is compatible with any ASP.NET and .NET Console Application project.
 
Today, I shall be demonstrating the integration of Asmak9.AuthorizeRESTWebApiAK .NET library with ASP.NET REST Web API platform.
 
Prerequisites
 
The following are some prerequisites before you proceed any further in this tutorial:
  1. Install Asmak9.AuthorizeRESTWebApiAK NuGet Package.
  2. Knowledge of REST Web API.
  3. Knowledge of ASP.NET MVC5.
  4. Knowledge of C# Programming.
The example of code is being developed in Microsoft Visual Studio 2019 Professional.
 
Let's begin now.
 
Step 1
 
Create a new Web API project and name it "RESTWebAuthorization".  
 
Step 2 
 
Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".
 
Step 3
 
Install Asmak9.AuthorizeRESTWebApiAK NuGet Package i.e.
 
 
Step 4
 
Open "Global.asax.cs" file and add following lines of code i.e. 
  1. RESTWebAPIAuthHeaderHandler obj = new RESTWebAPIAuthHeaderHandler(ApiInfo.API_KEY_HEADER, ApiInfo.API_KEY_VALUE, ApiInfo.USERNAME_VALUE, ApiInfo.PASSWORD_VALUE);  
  2.   
  3. // API authorization registration.  
  4. GlobalConfiguration.Configuration.MessageHandlers.Add(obj); 
The above code will register REST Web API authorization handler with the OWIN security layer of your REST Web API project base on your provided API key header name, API key value, authorize username and authorize password. If you have purchased the license key of this library then you can provide your license key in the last parameter i.e.
  1. RESTWebAPIAuthHeaderHandler obj = new RESTWebAPIAuthHeaderHandler(ApiInfo.API_KEY_HEADER, ApiInfo.API_KEY_VALUE, ApiInfo.USERNAME_VALUE, ApiInfo.PASSWORD_VALUE, "My_LICENSE_KEY");  
  2.   
  3. // API authorization registration.  
  4. GlobalConfiguration.Configuration.MessageHandlers.Add(obj); 
Step 5 
 
Lets, test out REST Web API in action using REST Web API client. I am using firefox plugin i.e. "RESTED". At, first, I simply try to hit the REST Web API without any authorization details and I will get following response, i.e.
 
 

 
Step 6
 
Now, I will provide the authorization and hit the REST Web API and will get the following response, i.e.
 
 
 

Conclusion

 
In this article, you learned to integrate Asmak9.AuthorizeRESTWebApiAK .NET library with ASP.NET REST Web API platform. You also learned to register the authorize handler provided by the library with your project OWIN security layer base on your API ley header name, API key value, authorize username and authorize password. If you have purchased the license key then you can pass your license key as last parameter during your object initialization. Finally, you learned to test REST Web API using any REST client to see your REST Web API in action.


Similar Articles