in Development

Licensing Applications with .NET

.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.

   1: [Flags]
   2: public enum MyLicenseRights
   3: {
   4:     None = 0,
   5:     Right1,
   6:     Right2
   7: }

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.

   1: [AttributeUsage(AttributeTargets.Class)]
   2: public sealed class MyLicenseRightAttribute : Attribute
   3: {
   4:     public MyLicenseRightAttribute() : this(MyLicenseRights.None)
   5:     {
   6:     }
   7: 
   8:     public MyLicenseRightAttribute(MyLicenseRights licenseRights)
   9:     {
  10:         _licenseRights = licenseRights;
  11:     }
  12:
  13:     public MyLicenseRights LicenseRights
  14:     {
  15:         get
  16:         {
  17:             return _licenseRights;
  18:         }
  19:     } MyLicenseRights _licenseRights;
  20: }

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”.

   1: public class MyLicenseProvider : LicenseProvider
   2: {
   3:     // To simplify the sample we just instantiate here a valid license
   4:     // it's up to you how and from you will retrieve your license
   5:     static MyLicense license = new MyLicense(
   6:         Guid.NewGuid(), "Test Customer", DateTime.Now.AddYears(1),
   7:         MyLicenseRights.Right1 | MyLicenseRights.Right2
   8:         );
   9: 
  10:     public MyLicenseProvider()
  11:     {
  12:     }
  13: 
  14:     public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
  15:     {
  16:         if (type == null)
  17:             throw new ArgumentNullException("Type");
  18: 
  19:         // Before to continue we verify the license is not expired
  20:         if (license.ExpiryDate > DateTime.UtcNow)
  21:         {
  22:             object[] attributes = type.GetCustomAttributes(typeof(MyLicenseRightAttribute), false);
  23: 
  24:             for (int i = 0; i < attributes.Length; i++)
  25:             {
  26:                 MyLicenseRightAttribute attribute = attributes[i] as MyLicenseRightAttribute;
  27: 
  28:                 if (attribute != null)
  29:                 {
  30:                     if ((attribute.LicenseRights & license.Rights) == attribute.LicenseRights)
  31:                         return license;
  32:                     else
  33:                     {
  34:                         break;
  35:                     }
  36:                 }
  37:             }
  38:         }
  39: 
  40:         if (allowExceptions)
  41:         {
  42:             throw new LicenseException(type);
  43:         }
  44:         else
  45:         {
  46:             return null;
  47:         }
  48:     }
  49: }

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.

   1: [LicenseProvider(typeof(MyLicenseProvider))]
   2: [MyLicenseRight(MyLicenseRights.Right1)]
   3: public class TestRight1
   4: {
   5:     public TestRight1()
   6:     {
   7:         LicenseManager.Validate(typeof(TestRight1), this);
   8:     }
   9: 
  10:     public string GetMessage()
  11:     {
  12:         return "License Right 1 granted to access";
  13:     }
  14: }

That’s all, to test just try to modify the Rights I’ve set to MyLicense field in the MyLicenseProvider class.

LicenseTest.zip