.NET has a feature from the version 1.0 that allows licensing the components you build. This licensing model can be used in the same way for Windows and ASP.NET server controls and can be easily extended.
The way in which it works is so simple, you only need to follow these steps:
- Apply the attribute LicenseProviderAttribute to the classes that need to be licensed.
- Call Validate in the constructor.
- Dispose the license.
To do it you need to work basically with only three types that are in the namespace System.ComponentModel:
- LicenseManager, which is used to validate if a component is licensed or not.
- LicenseProvider. It’s the abstract class that allows retrieving the licenses .NET Framework has already an implementation of this class: LicFileLicenseProvider, which bases its license model on text files and “provides similar licensing functionality to COM licensing”.
- License. Abstract class used as the base to create your own licenses.
In this post we will create an extended License model based on attributes, similar to how it can work the declarative security in .NET, this will allow us setting which license rights are necessary to use our application and to abstract better the Licensing logic from the logic of our application.
The first we do is to define all the rights that our license will be able to protect, in this case we set them in an enumeration called MyLicenseRights.
The next step is to write the custom attribute we will use to check what are the license requirements in order the type can be used.
Now we are ready to create the class License, this class has only one property you need to override, this is the “LicenseKey”, but you can add as much information as you want. For this sample and for demo purposes we will add the name of the Customer we have licensed, an Expiry Date and the “MyLicenseRights” we have granted to the customer.
Now that we have the attribute we will apply to our class to specify the required license rights and our license, we must implement the last piece of the puzzle: the LicenseProvider that will check if a class can be used or not. Again the logic behind is very easy, you only need to override the method “GetLicense”, this method must return a valid License if the type we check is licensed otherwise we return null or an exception depending on the value of the parameter “allowExceptions”.
As you can see in the code above, to simplify the example, I have not added the logic to retrieve the licenses. I’ve just instantiated a static field, that contains the MyLicense details. Of course you will need to add the logic to handle how to retrieve your license information.
Finally we only need to do two steps more, the first is to protect our classes by applying the standard LicenseProviderAttribute and the MyLicenseRightAttribute we just created. The second is to validate the license as we mentioned at the beginning of the article using the method IsValid or Validate. The next code shows you how to do it.
That’s all, to test just try to modify the Rights I’ve set to MyLicense field in the MyLicenseProvider class.