C#.NET - Generate An API Key Using Nuget Library

Introduction

API keys are used in the authorization process of either a web product, desktop application, or mobile application; therefore, the generation of a secure API key typically requires a combination of a password and a secret/salt key. 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. The library also provides a facility to generate a random API key.

Today, I shall be demonstrating the integration of theAsmak9.AuthorizeRESTWebApiAK .NET library with a C#.NET Console Application.

Prerequisites

The following are prerequisites before you proceed in this tutorial.

  1. Install Asmak9.AuthorizeRESTWebApiAK NuGet Package.
  2. Knowledge of C# Programming.

The example code is being developed in Microsoft Visual Studio 2019 Professional.

Let's begin now.

Step 1. Create a new C#.NET Console Application project and name it "GenerateApiKey".

Step 2. Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".

Step 3. Install Asmak9.AuthorizeRESTWebApiAK NuGet Package i.e.

application installation

Step 4. Open the "Program. cs" file and add the following lines of code i.e.

// Initialization.
string myPassword = "mypassword";
string mysaltKey = "mysaltkey";
// Generate API key.
string apiKey = RESTWebAPIKey.GenerateAPIKey(myPassword, mysaltKey);

The above code will generate an API key value based on your provided password and secret/salt key values. If you have purchased the license key of this library, then you can provide your license key in the last parameter, i.e.

// Initialization.
string myPassword = "mypassword";
string mysaltKey = "mysaltkey";
// Generate API key.
string apiKey = RESTWebAPIKey.GenerateAPIKey(myPassword, mysaltKey, "MY_LICENSE_KEY");

Step 5. Now, execute the project, and you will be able to see the following output.

Conclusion

In this article, you learned to integrate Asmak9.AuthorizeRESTWebApiAK .NET library with C#.NET Console Application. Finally, you will also learn to generate an API key value based on your provided password and secret/salt key values. If you have purchased the license key, then you can pass your license key as the last parameter in the API key generation method.


Recommended Free Ebook
Similar Articles