Header Ads

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

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 integration of Asmak9.AuthorizeRESTWebApiAK .NET library with ASP.NET REST Web API platform.

Prerequisites:

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 code is being developed in Microsoft Visual Studio 2019 Professional.

Download Now!

Let's begin now.

1) Create new Web API project and name it "RESTWebAuthorization".  
2) Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".
3) Install Asmak9.AuthorizeRESTWebApiAK NuGet Package i.e.


4) Open "Global.asax.cs" file and add following lines of code i.e.

            RESTWebAPIAuthHeaderHandler obj = new RESTWebAPIAuthHeaderHandler(ApiInfo.API_KEY_HEADER, ApiInfo.API_KEY_VALUE, ApiInfo.USERNAME_VALUE, ApiInfo.PASSWORD_VALUE);

            // API authorization registration.
            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.
            RESTWebAPIAuthHeaderHandler obj = new RESTWebAPIAuthHeaderHandler(ApiInfo.API_KEY_HEADER, ApiInfo.API_KEY_VALUE, ApiInfo.USERNAME_VALUE, ApiInfo.PASSWORD_VALUE, "My_LICENSE_KEY");

            // API authorization registration.
            GlobalConfiguration.Configuration.MessageHandlers.Add(obj);

5) Lets, test out REST Web API in action using REST Web API client. I am using fire fox 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.


 

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


 

 


Conclusion

In this article, you will learn to integrate Asmak9.AuthorizeRESTWebApiAK .NET library with ASP.NET REST Web API platform. You will also learn 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 and finally, you will also learn to test REST Web API using any REST client to see you REST Web API in action before consumption.

1 comment:

  1. Thanks For sharing valuable Information....

    Movie box is excellent application for them who love to enjoy
    their time with movies and TV series.There was a time when watching
    and downloading movies.
    movies box apk
    moviebox download android

    ReplyDelete