Automatically issue trial licenses

This blog post will focus on the Verified Trial feature in Cryptolens, allowing you to automatically issue trial licenses in Cryptolens. The full documentation page can be found here.

You can also watch this tutorial as a YouTube video:

Let’s first cover the theory behind the Verified Trial feature. In your application, you would call a specific API method, in this case the Key.CreateTrialKey method, and supply the machine code or any other device identifier. As long as that machine code has not received a trial key before, a new trial key will automatically be generated and sent to the application.

Setting up the trial licenses in Cryptolens

To implement the Verified Trial feature, you would need to add additional code to your product. Please find this code example at the bottom of this blog post, or on the dedicated documentation page.

To set this up, we will begin with a limited version of the code so you can make sure that you get everything properly set up before you implement the feature. To get started, simply copy the code below and paste it into your application.

var newTrialKey = Key.CreateTrialKey("access token", new CreateTrialKeyModel {ProductId= "product id", MachineCode = "machine code" });

if(!Helpers.IsSuccessful(res))
{
	Console.WriteLine(res?.Message);
}

Console.WriteLine(res.Key);

For this to work, you need to enter an access token, your product ID, and the machine code into the code above.

Access Token

For the access token, you need to sign in to Cryptolens. Go to the Access Token page and click “Create new Access Token”.

Give your new access token a name. Under the “License Key” headline, click on the checkbox called “Create Trial Key”. To complete, click on “Create an Access Token”

At the top of the Access Token page, a green field will appear with the token you have just created. Copy the access token, either manually or by clicking on the blue icon to the right of the access token.

Paste the access token in your application’s code where it says “access token”.

Product ID:

Make sure you provide the correct Product ID of your desired Cryptolens product in your application’s code. You find the Product ID by going to the product’s page in Cryptolens. The Product ID should be visible just above the “Create a new key” button.

In the code you just pasted into your application, please provide the correct Product ID where it now says “product id”.

Machine Code:

Normally, you would use Helpers.GetMachineCode to get the correct machine code. For this example, however, you may specify your own machine code to make sure everything is working, and when you want the Verified Trial feature to go live, you should simply replace your example machine code with Helpers.GetMachineCode.

For this example, we will set the machine code to “test3” by entering it into the code where it says “machine code”.

Final Step:

Run the application. If everything works as it should, the newly created trial license key should appear. When the feature is live, you should proceed as normal with key verification after this step.

You can now find the trial key on the Product Page, just like it does when you create the key manually.

You have almost learned how to automatically issue trial licenses in Cryptolens. We will now cover a way of customizing the pre-defined features and the expiration date. If you do not wish to make any changes, proceed with the section of this blog called “Full implementation”.

Customizing the pre-defined features

Notice how the new key has the “trial” feature set as true. Unless you have specified that all licenses are time-limited, the trial key should also have another feature set as true, which is the “time-limited” feature.

If you want to change which feature is the trial and time-limited feature, click on Edit Feature Names. 

In Feature Definitions, you can specify which features you want to be the trial and time-limited feature. If you want all licenses to be treated as time-limited, you can click on the checkbox below the list of 8 features called “Treat all licenses as time-limited”.

Customizing the expiry date

Notice how the trial key you created will expire in 15 days. You can customize this by changing the access token.

Go to the Access Token page and delete the access token you created earlier in this tutorial by clicking on the cross icon to the right for the correct access token.

Click on Create new Access Token. Give the access token a name. Once again, select the “Create Trial Key” checkbox in the License Key section.

Scroll all the way down to the bottom of the page to find the Feature Lock field. In this field, you can specify the expiry date of the automatically issued trial license keys. If you want the trial license key to be valid for 30 days, simply write 30 in the Feature Lock field.

When you have specified your desired expiration date, click on Create an Access Token.

Copy the new access token by finding the green field at the top of the Access Token page as you did earlier and replace the old access token in your application’s code with the new one.

Remember to change the example machine code you provided earlier to see if everything is working. For this example, we will change the machine code to “test5”.

Run the application again. If everything works, the new trial license should appear.

Go back to the Product page and you should find the newly created trial license key with the expiry date that you specified in the access token’s Feature Lock field. For this example, the new key should be valid for 30 days.

Full implementation

To fully implement this feature, please see the code example below.

Notice that the first part is exactly the same as we pasted earlier, meaning that if you have followed the steps in this blog, that part is correctly customized for your application.

Lastly, remember to remove your example machine code and instead use Helpers.GetMachineCode, or any other method of identifying devices, as soon as you are done experimenting with the Verified Trial feature.

Here is the code example for full implementation:

var newTrialKey = Key.CreateTrialKey("access token", new CreateTrialKeyModel {ProductId= "product id", MachineCode =Helpers.GetMachineCode() });

if(newTrialKey == null || newTrialKey.Result == ResultType.Error)
{
    Assert.Fail("Something went wrong when creating the trial key");
}

var activate = Key.Activate("access token", 
    new ActivateModel {
        ProductId = 3941,
        Sign = true,
        MachineCode = Helpers.GetMachineCode(),
        Key = newTrialKey.Key, Metadata = true
    });

if(activate == null || activate.Result == ResultType.Error)
{
    Assert.Fail("Something went wrong when verifying the trial key");
}

// now we can verify some basic properties
if (Helpers.IsOnRightMachine(activate.LicenseKey) && activate.Metadata.LicenseStatus.IsValid)
{
    // license verification successful.
    return;
}

Assert.Fail();

Now you know how to automatically issue trial licenses in Cryptolens.

Thank you for reading this blog, please reach out if you have any questions to [email protected].


Not a customer yet? Sign Up for a free trial and implement our software licensing system within minutes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.