License Activation

License activation is a commonly used method to control the distribution and usage of software licenses. With Babel Licensing, you can leverage the License Activation feature to securely distribute and activate licenses for your .NET applications. In this introduction, we will explore how to create a license using the Babel User Interface (Babel UI) on the Babel Licensing Service and how to configure a client .NET application to activate the license using the Babel Licensing library.

Prerequisites

To enable license activation functionality in Babel Licensing Service, follow these steps. First, ensure that Babel Licensing Service is up and running on your server according to the instructions provided in the Getting Started section. Then, create a test product and a corresponding template license by referring to the instructions provided below. This will establish the foundation for generating activation licenses.

Next, you'll need to download the license activation client source code from GitHub. This client code will be used to integrate license activation capabilities into your .NET application.

git clone https://github.com/babelfornet/license-activation-console-example.git

The creation of a floating license for this example requires the creation of a corresponding license template. To create the license template for your test client, please refer to the instructions outlined in the "Creating License Template" section. Follow the provided steps to ensure the successful creation of the license template required for generating the floating license.

Creating an Activation License

The Babel User Interface provides a user-friendly interface for license management. You can easily create and configure licenses for your applications through this interface. You can specify various details for the license, such as the product name, version, expiration date, and allowed features. Additionally, you can set activation restrictions, such as the maximum number of activations or the specific machines or domains on which the license can be activated.

To register an activation license in the Babel User Interface, follow these steps:

  1. Launch the Babel User Interface and navigate to the Data section.

  2. Click on the "Licenses" link to display the list of existing licenses.

  3. Click on the "New License" button on the grid header to create a new license.

  4. In the License Template combo box, select the license template created in the previous paragraph.

  5. The License fields will automatically populate with the information from the selected template.

  6. In the "Licensing Mode" field, select "Activation" as the licensing model for the license.

  7. In the "Allowed Sites" column, enter the desired number of activations that will be available for this license.

  8. Generate a new User Key by pressing the arrow button located at the right end of the "User Key" field. This user key will be used to activate the license on a specific machine.

  1. Enter the necessary details for the license, such as the licensee information and, eventually, any additional field or restriction you want for this license.

  2. Select the desired license format from (XML, XML (dsig), ASCII, BASE32) and press the Generate button to generate the license.

  3. Then save by clicking the "Save" button.

The public key extracted from the Keys.snk file, which is necessary for license validation, is already configured in the example source code.

  1. The newly registered activation license will now appear in the licenses grid with a unique license key and other relevant information.

Once the activation license is registered, the customer can activate the license on their machine by providing the user key and, if applicable, the machine ID. The Babel Licensing Service will validate the information and issue a license token tied to that specific machine, allowing the software to run.

Configuring a Client

Once the license is created on the Babel Licensing Service, you need to configure your client .NET application to activate the license. This involves integrating the Babel Licensing library into your application and implementing the necessary code to activate the license. The Babel Licensing library provides a set of APIs and methods that streamline the license activation workflow and ensure secure license validation.

To open the license activation sample project, follow these steps:

  1. Navigate to the location where the license activation sample project is stored and double-click on the ClientActivation.sln solution file to open it in your preferred Integrated Development Environment (IDE), such as Visual Studio.

  2. Once the project is open, ensure that the Babel Licensing client library is referenced correctly. If the reference is missing, please refer to the Client Components paragraph to configure a local NuGet repository.

  3. After ensuring the correct references, build the solution by selecting the "Build" or "Rebuild" option from the IDE's menu.

  4. Once the solution is successfully built, you can run the application in debug configuration by pressing the "Start Debugging" button or using the keyboard shortcut specific to your IDE (e.g., F5 in Visual Studio).

  5. The license activation sample application will launch, allowing you to interact with the licensing functionality and test the license activation process.

  1. Enter the activation key generated for the license and press ENTER.

If the activation key is valid and matches the one set for the license, the license will be successfully activated.

You can now deactivate the license or close the application by pressing "n".

  1. Press "n" then ENTER to close the application.

If you restart the application, you will see that the license is still active. This is because the client has been configured to maintain a local cache of the license information. The client will only contact the server once every 5 days to perform remote license validation. This caching mechanism allows the application to continue functioning even if the connection to the server is temporarily unavailable.

Follows the complete license activation example source code:

using Babel.Licensing;

// Create a new configuration object
BabelLicensingConfiguration config = new BabelLicensingConfiguration() {
    // Set the service URL
    ServiceUrl = "http://localhost:5005",

    // Set the public key used to verify the license signature
    SignatureProvider = RSASignature.FromKeys("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDE1VRiIdr6fiVZKve7NVgjIvGdRiRx0Mjjm+Yzf6tLbzFnxLs0fat5EoRcubxx0QQQDfydsJBE/fc7cwRWSrE2xK6X4Eb4W8O47pCMjqvTQZfDqQywEZJrLlxpp9hlKz6FDYX4SagrjmP1gdw8olo+n+IBz8ubkNxRhvycikxuDQIDAQAB"),
    
    // Set a unique client ID
    ClientId = "my app client id"
};

// Create the client object used to communicate with the server
BabelLicensing client = new BabelLicensing(config);

// Use BabelServiceLicenseProvider to cache a local copy of the license for offline use
BabelServiceLicenseProvider provider = new BabelServiceLicenseProvider(client) {
    // Refresh the license by contacting the server every 5 days
    // If set to TimeSpan.Zero, the license will be validated every time on the server
    LicenseRefreshInterval = TimeSpan.FromDays(5)
};

// Register the license provider with BabelLicenseManager
BabelLicenseManager.RegisterLicenseProvider(typeof(Program), provider);

// Check if the user key was set
if (provider.UserKey == null)
{
    // If the user key was not entered, the license has not been activated yet.

    // Ask the user to activate the license
    Console.WriteLine("Please enter your activation license key: ");
    string? userKey = Console.ReadLine();

    try
    {
        await client.ActivateLicenseAsync(userKey, typeof(Program));

        Console.WriteLine("License activated.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Could not activate license: {ex.Message}");
        return;
    }
}

try
{
    // Validate the license
    // This will contact the server to validate the license if the local copy is expired
    // You can customize the validation interval using the LicenseValidationInterval property
    var license = await BabelLicenseManager.ValidateAsync(typeof(Program));

    Console.WriteLine($"License {license.Id} valid.");

    // Ask to deactivate the license
    Console.WriteLine("Do you want to deactivate the license? (y/n)");
    string? answer = Console.ReadLine();

    if (answer?.ToLower() == "y")
    {
        try
        {
            await client.DeactivateLicenseAsync(provider.UserKey);
            Console.WriteLine("License deactivated.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Could not deactivate license: {ex.Message}");
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"License not valid: {ex.Message}");
    return;
}

The BabelLicensing class is instantiated to communicate with the licensing server. The public key to validate the license is assigned in the service configuration, as well as the URL to communicate with the service.

Then, an instance of the BabelServiceLicenseProvider class is instantiated and registered with the BabelLicenseManager. This is responsible for caching a local copy of the license for offline use. The LicenseRefreshInterval property is set to determine how often the license should be refreshed from the server.

The code checks if the user key (activation license key) was entered. If it's not set, it means the license has not been activated yet.

If the user key is not set, the code prompts the user to enter the activation license key and attempts to activate the license by contacting the server.

The code then proceeds to validate the license using the ValidateAsync method of the BabelLicenseManager. This method checks the validity of the license, and if the local copy of the license is expired, it contacts the server for validation.

After validating the license, the code prompts the user to confirm whether they want to deactivate the license. If the user confirms the deactivation, the code calls the DeactivateLicenseAsync method of the BabelLicensing client to deactivate the license.

Overall, the provided code demonstrates how to interact with the Babel Licensing client library to handle license activation, validation, and deactivation in a client application.

The license activation process provides several benefits, such as enhanced security, as it ensures that only authorized users can activate and use the licensed software. It also enables you to track and manage license activations, providing valuable insights into the usage patterns of your application. By leveraging the License Activation feature in Babel Licensing, you can ensure compliance with licensing terms and maintain control over the distribution and usage of your software.

In conclusion, license activation with Babel Licensing empowers you to distribute and activate licenses for your .NET applications securely. By creating licenses using the Babel User Interface and configuring your client application for license activation, you can enforce license compliance, protect your intellectual property, and provide a seamless licensing experience for your users.

Last updated