Licensing in .NET explained - Part 2

In the first part of this article, we’ve seen how to deal with licensed components from the user point of view. In the second part, I’ll try to explain the whole licensing process, that is:

  1. Authoring licensed components.
  2. Creating licences at design-time.
  3. Creating licences at compile-time.
  4. Creating licences at run-time.

Authoring licensed components

I’ll not spend too much time on that topic, as it’s widely documented on the web (see for example this article).

The process is simple. After creating your component, you have to:

  1. Apply a LicenseProviderAttribute to the class.
  2. Call LicenseManager.Validate from the class constructor to get a licence.
  3. Dispose the licence in the component finalizer.

The licence provider specified by the LicenseProviderAttribute attribute in step 1 is used during step 2: Calling LicenseManager.Validate in your component constructor will create an instance of this license provider, and will call its GetLicense method to get a licence. If no licence can be found, LicenseManager.Validate will throw an exception, making your component unusable.

Specifying the licence provider has also another usage: It’s used by Visual Studio to know whether a component is licensed or not. Indeed, as we’ve seen in the first part of this article, Visual Studio manages a licenses.licx file containing all the licensed components used in a project.

Microsoft provides the LicFileLicenseProvider class, a ready-to-use licence provider. But if you need to implement your own logic, you have to create a new licence provider by subclassing the LicenseProvider class. That’s what we’ve done here at ILOG for our .NET products (ILOG Gantt for .NET and ILOG Diagrammer for .NET) because we wanted to implement ILM (ILOG License Manager). Subclassing the LicenseProvider class is relatively simple, as you just need to override the GetLicense method. We’ll see later how to implement this method.

The LicenseProvider will be used in three different contexts:

  • At design-time.
  • At compile-time.
  • At run-time.

Creating licences at design-time

Here we assume that we’re building an application that contains licensed components using Visual Studio. The typical way to do it is to drag and drop a toolbox item onto the design surface, which results in the instantation of the component. As the licence checking is done in the component constructor, a licence has to be provided at that time. Let’s see how to handle this case in the LicenseProvider.GetLicense method through a partial implementation:

public override License GetLicense(
    LicenseContext context,
    Type type,
    object instance,
    bool allowExceptions)
{
    //
    // The context parameter indicates the context
    // in which the component is used.
    //
    bool runtime = context.UsageMode == LicenseUsageMode.Runtime;                 

    if (runtime)
    {
        //
        // Will see later what to do here
        //
    }
    else
    {
        //
        // Here we are designing our application, so we need to
        // get a licence that we will store in the context.
        //                 

        //
        // We're calling the GetDesignTimeLicenseKey() method to get a licence.
        // The code for this method is not provided here, but a simple
        // implementation could be to look in the registries or in a specific
        //  file to check that a valid licence has been installed on this computer.
        // The implementation of this method depends on the licensing schema
        // you want to set up.
        //
        string licenseKey = GetDesignTimeLicenseKey(type);                 

        //
        // If no licence was found, error
        //
        if (licenseKey == null)
        {
            if (allowExceptions)
                throw new LicenseException();
            return null;
        }                

        //
        // Save the licenseKey in the context. It will be possible at runtime to
        // retrieve it.
        //
        context.SetSavedLicenseKey(type, licenseKey);                

        //
        // We create and return a new CustomLicence object using
        // the licenseKey. The code for the CustomLicense class is
        // straightforward, as it's a simple implementation of the License
        // class. It's given below.
        //
        return new CustomLicense(licenseKey);
    }
}               

class CustomLicense : License
{
    private string _key;
    public CustomLicense(string Key)
    {
        _key = key;
    }
    public override string LicenseKey
    {
        get
        {
            return _key;
        }
    }
}

To sum up, the creation of a licence at design time is a 3 steps process:

  1. Get the licence key installed on the computer. It can be a licence file, or a registry key…It depends on the way you want to manage the installation of licence keys on development computers.
  2. Sets the licence key into the licence context. This will make it possible to retrieve the licence key at runtime.
  3. Create a License object using this licence key.

Note : The License class is a .NET class that encapsulates the licence key, which is a simple string.

The LicenseContext used by Visual Studio and passed as the first parameter of the GetLicense method is said to be a “design-time” context, that is, a context whose UsageMode property returns LicenseUsageMode.DesignTime.

Creating licences at compile-time

Once the application has been designed, it needs to get compiled. As we’ve seen in the first part of this article, Visual Studio invokes the licence compiler to compile the licenses.licx file into an assembly resource file.

The licence compiler takes a .licx file as input and produces a resource file as output. It parses the .licx file and create an instance of each type found in it. The LicenseContext used by the licence compiler is a design-time context, an instance of the DesignTimeLicenseContext class. For each component created, a corresponding licence is also created and is stored in the licence context in a hash table. This hash table is then dumped it into a resource file.

This resource file will be used differently at run-time depending on the application type your’re creating, as explained in the first part of this article.

Creating licences at run-time

As for design-time and compile-time, licence checking will take place at run-time each time a licensed component gets instantiated. The difference is that the LicenseContext used will be a run-time licence context, that is, a context whose UsageMode property returns LicenseUsageMode.RunTime. This context will be able to retrieve the licences compiled with the licence compiler. Here is the a partial implementation of the LicenseProvider.GetLicense method:

public override License GetLicense(
    LicenseContext context,
    Type type,
    object instance,
    bool allowExceptions)
{
    //
    // The context parameter indicates the context in
    // which the component is used.
    //
    bool runtime = context.UsageMode == LicenseUsageMode.Runtime;                 

    if (runtime)
    {
        //
        // At runtime, look into the context to find the licence.
        // Depending on the application type, the context may look into
        // the application resources (Windows Forms), or the
        // App_Licenses.dll assembly (Web sites)...
        //
        string licenceKey = context.GetSavedLicenseKey(type, null);                

        if (licenceKey != null)
            return new CustomLicense(licenceKey);           

        if (allowExceptions)
            throw new LicenseException();
        else
            return null;
    }
    else
    {
        //
        // Already covered in the "design-time" section
        //
    }
}

The interesting part here is the call to the LicenseContext.GetSavedLicenseKey method. It will look into the context to see if a licence for the specified type can be found. Depending on the application type (assembly based applications, web sites, windows forms hosted in IE), the context will look in the appropriate location to find the licence.

Conclusion

We’ve seen in this article how the licences were created during the three phases of an application development: design-time, compile-time, run-time. Of course the code given was simpified for the purpose of this article, but you can imagine to enrich it by adding cryptography or anything that can make your licence management more secure.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

2 Responses to “Licensing in .NET explained - Part 2”

  1. Sajid Says:

    Hi. I whole code.plz rely soon. i dont know how we implement it

  2. Robert Dupuy Says:

    Hi,

    What code do you need ? The code posted in the article can be used as a base for developping your own licensing mechanism. I can help you if you tell me where do you have a problem in your license provider implementation.

    Robert.

Leave a Reply