Rune Grønkjærs Blog
Abonnér på mit feed

How to create a license webshop

Many small or medium-sized developers have created software products they want to sell. Maybe they sell them already, but everything is done manually and may lose some customers who thought it too difficult or who is not familiar with the product. The solution is often to sell his products in a webshop. The problem is that they are often too costly, too cumbersome or simply do not meet the requirements.

I have in collaboration with Anders Burla Johansen developed an extension to Tea Commerce, which makes it easier than easy to get your own license webshop up and running. On this page you can find everything you need to do to make your own license webshop.

Goal

I will find a simple but effective way to license a piece of software. I will focus on being able to sell licenses for Web software, which must be limited to a top level domain. It should be possible with a few adjustments to make licenses that are limited in other ways or who works for eg desktop applications. It would be quite up to the individual developer to customize this blog posts examples for the specific task he / she faces.

I will also create a License Kit package for Umbraco which should provide a simple and good foundation for a license webshop. The License Kit package will act the same way as the Tea Commerce Starter Kit’et . One should therefore be able to install the JavaScript, xslt’s, document types, etc. that will be needed in the final license webshop.

Once you have gone through this guide you will be left with a shop that can sell yours or others’ licenses. Go directly to the tutorial

Assumptions

Before you get started you must first have a clean Umbraco solution with a installation of Tea Commerce. This tutorial support Umbraco 4.5+ and Tea Commerce 1.2.1.1+.

The solution

The solution consists of 4 different products, each of which solves a small part of the problem. You can choose to use all four of them, modify them according to your needs and then you have a setup that can sell your licenses. One is a package for Umbraco and can be downloaded here (TODO: link properly). The last three products are assembled in this Visual Studio Solution (TODO: link properly). In the following I will briefly review the four products.

The four products for this tutorial

  1. License Kit package for Umbraco
  2. Asymmetric-key generator
  3. License generation for Tea Commerce
  4. License check for the product

License Kit package for Umbraco

For a quick start, I made the License Kit Package for Tea Commerce and Umbraco (TODO: link correctly ). It installs a starter kit on your Umbraco solution and make sure to do the most basic configuration changes in the Tea Commerce.

With License Kit’et you get:

  1. Changes to Tea Commerce’s basic configurations
  2. Document Types
  3. Templates
  4. CSS
  5. JavaScript
  6. XSLT’er and macros
  7. Pages and product in the content section
All targeted a licensed shop and with all the best practices we have found in Tea Solutions.
License Kit Content Tree
The content tree after installation

Asymmetric-key generator

Project: TeaCommerce.LicenseKit.AsymmetricKeyGeneration
This small project is extremely simple and is as said to be found in the Visual Studio. NET solutions. If you build it, it generates two XML files that is saved to the root of your C drive. The one file holds the secret private key, the second one holds the not so secret public key. Where and how to use them I will come back to later.

License generation for Tea Commerce

Project: TeaCommerce.LicenseKit.Webshop
This class library is an extension to Tea Commerce, which allows the webshop to generate the licenses. In it simply hooks into the event AfterOrderFinalized, which is fired once the order has been paid for and finalized. In my setup it generates a license file and attaching it to an e-mail which is then send it to the customer.
A small LicenseHelper class, that generates the license key, is also in this project. This is where you can change the licensing method.

License check the product

Project: TeaCommerce.LicenseKit.Product
This project is your product. Or it holds the code you will be putting in your product. In my example the product can write if it is licensed or not. Includes a LicenseHelper class, which in this case checks for valid licenses. With a single method call, you can check if the product is licensed. This functionality can be added in any .NET website product.

Tutorial – This is it

Follow these tutorial steps to setup your own shop.

1. Download the stuff to use in this tutorial

As mentioned above, I have created a License Kit and Anders Burla Johansen have made three. NET projects that can help you get started. You need to download. (TODO: Do links correct)
  1. Tea Commerce License Kit package for Umbraco
  2. LicenseKit prosjekter (. NET VisualStudio solution)

2. Install License Kit Packages

You install the License Kit as you do any other Umbraco package. Remember that you must install this package on a clean Umbraco installation where you have already installed Tea Commerce v1.2.1.1+.
After installation, you can check if it works by adding a license to the cart and complete the order. At present, it will NOT generate any license files. You will be able to see that the order goes through as it should, and we now just need to generate license files.

3. Generate a private public key pair

Now we need to generate a private / public key pair. To do this, start the solution that you just downloaded, in Visual Studio 2010. The project TeaCommerce.LicenseKit.AsymmetricKeyGeneration have the functionality you need. Running the project will save two xml files to your C drive.

One file is your private key, the second is your public key. The private key file must be copied into your web shops App_Data folder. Then type the name of the file on the product’s “License Key Private Key File” property in the Umbraco content. I’ve already written the default name, so unless you change the name of the file, you need not do more.
We do not need the public-key file yet. It will be copied into your product later.

NOTE: For each of your products you will need to generate a unique key pair, as mentioned above. That way, you can not reuse a license between two different products.

The theory behind

To protect your product copyrights we are creating a product license, which the product will be able to recognize. By signing the license with our private key we can verify it with our public key. That way we can read the contents of the license in our product and make sure that the product is licensed. The method is relatively safe since it will be difficult to break the encryption without the private key. It is simple because the product does not need to check against a web service to be authenticated.

Settings you can change

You can choose to change a few things in this project. The most important thing is that you have the two generated keys. What you call the files or where and how to store them is subordinate.
NOTE: Please note that if you change the private key file name or location on your webserver, you will also need to change it in the TeaCommerce.LicenseKit.Webshop project where it reads the file.
NOTE: your private key MUST be secure. It must, therefore, under no circumstances be downloadable from the web shop. Therefore we have chosen to store it in the App_Data directory, which is secured by the IIS.

4. Extending the webshop

Now we need to make our webshop generate licenses. To do this, we have built the TeaCommerce.LicenseKit.Webshop project. The DLL you get by building the project should be uploaded to the webshop’s bin folder. Basically, this is enough. Now your webshop can generate licenses.
The small extension will also generate an email in which the license keys are attached. The email body will be generated with Tea Commerce’s own InvokeXSLT which is used to retrieve the contents of a specific xslt file. You can edit the xslt file and write a decent text or whatever you want.

You can now test if it works by running another order through. As long as the website has access to an SMTP server, it will now send an email with the license keys. NOTE: Remember that there is a reference to the Tea Commerce DLL TeaCommerce.Data in this project. It must be the same as the one you have on the web shop.

The Theory behind

The project has a LicenseHelper class. This is the one that generates the license keys with your private key, which it obviously needs to have access to.
A text that can be pulled out and used to verify the license, will be saved in the license. In this text I have pasted the domain that the user enters when he adds the license to the cart. In front of the domain is prefixed “0001″. This is a device designed to ensure that we can generate other types of licenses in the future. This could eg be a server license with the prefix “0002″. The idea is that the product must check this four-digit number and then know which method it must use to check the license .

Settings you can change

You can change several parameters in this project.
  • E-mail – You can change as much as you want on the email being sent. In the example we have hardcoded the subject, send, etc., but you can retrieve this information from other sources if you like.
  • E-mail body – The name of the xslt file is hardcoded. You can change this or find another way to generate the e-mail body.
  • Private key file name – In this example the name of this filen comes from the product node where it has been entered. You could also hard code the location or find another way to link a product along with its private key. The important thing is to keep the private keys secure. The way I’ve done it is safe, but also flexible, as you could easily create new licensed products.
  • The name of the license file – Right now I call the license file for “[productName]-[domain].lic”. You can call it anything, as we check all *.lic files to find a proper license, in the product project.
  • Private key – You can choose to move or rename your private key file. You may also put the file’s contents directly into the code itself.
  • productSaltLength – Must just fit with the salt that is used to decrypt the license

5. Check the license in your product

Now we have a license key to be tested. For convenience, you can choose to test it directly on the webshop site. In the .NET solution there is a TeaCommerce.LicenseKit.Product project, which will make it up for your product.
The first thing you do is copy the contents of your public key xml file, located in the root of your C drive.
In the LicenseHelper class the public key content must be deployed in the constant “publicLicenseKey”.

Once you’ve done this rebuild you project and copy the DLL into the bin folder and LicenseCheckTest.ascx into the usercontrols folder. The LicenseCheckTest.ascx is “your product” that we will now use on the site. You do this by creating a new macro and choose LicenseCheckTest.ascx in the usercontrol dropdown. Click “Use in editor” to be able to insert the macro in the content of one of your pages.
Now open the page you just put the new macro on. If it says “Domain is licensed” you have done everything correctly. If not, just check the previous steps in this tutorial again.

The theory behind

In LicenseCheckTest.ascx.cs you can see how easy the license check is done in the product itself. LicenseHelper.HasLicense() returns true if there exists a valid license. Where and how you use this method in your product is entirely up to you.
In the LicenseHelper class you can see how the check will be made. Once the class is initialized the licenses are saved in a static variable, from which they can be retrieved later.
The HasLicense method is checking the stored licenses against the domain of the site and returns true as soon as a match is found.
Note that there is a switch/case in which we investigate what type of license we are checking. In this the license is handled according to it’s prefix number.

Settings you can change

  • publicLicenseKey – The public key is hardcoded into the project, but could store it on the server as a file. Depending on the situation it is convenient to keep it in the code though, as everything will be in one place.
  • productSaltLength – Must just fit with the length of the salt that is used to encrypt the license
  • Location of license file – In this example we place it in the bin folder. It may be placed wherever you like as long LicenseHelper can get to its contents.

Many thanks to

My colleagues Anders Burla Johansen and Morten Vejhe. Anders for his huge work with the license helpers and Morten for his knowledge about all the weird stuff.

Dansk version