Licensing Components and Controls

The .NET Framework provides a licensing model that is identical for all components (including Windows Forms controls and ASP.NET server controls) and is fully compatible with licensing for Microsoft ActiveX® controls.

Licensing allows control authors to help protect intellectual property by checking that a user is authorized to use the control. This check is more important at design time, when the control is incorporated into an application, than at run time. When a licensed control is used legally at design time (and it cannot be used otherwise), the application gets a run-time license that can be freely distributed.

The licensing model also allows many other levels of licensing support by separating the validation logic from the component or control. The granting of licenses and the validation logic is performed by a license provider, which is a class that derives from System.ComponentModel.LicenseProvider. The steps that a component author must take to enable licensing are straightforward.

To enable licensing for your component

  1. Apply a LicenseProviderAttribute to the class.
  2. Call LicenseManager.Validate or LicenseManager.IsValid in the constructor.
  3. Call Dispose on any granted license in the finalizer of the class or before the finalizer is called.

The following example shows a Windows Forms control that implements a simple case of licensing.

using System;
using System.ComponentModel;
using System.Windows.Forms;
public class MyControl : Control {
    private License license = null;
    public MyControl () {
        license = LicenseManager.Validate(typeof(MyControl), this);
    }
    protected override void Dispose(bool disposing) {      
       if (disposing) {
          if (license != null) {
            license.Dispose();
            license = null;
          }
       }
      base.Dispose(disposing);
    }
    ~MyControl() {
        Dispose();
    }
}
[Visual Basic]
Imports System
Imports System.ComponentModel
Imports System.Web.UI

<LicenseProvider(GetType(LicFileLicenseProvider))> Public Class  MyControl 
Inherits Control
    Private license As License
    
    Public Sub New()
        license = LicenseManager.Validate(GetType(MyControl), Me)
    End Sub
    
    Public Overloads Overrides Sub Dispose()
        If Not (license Is Nothing) Then
            license.Dispose()
            license = Nothing
        End If
    End Sub
End Class

The following example shows an ASP.NET server control that implements a simple case of licensing.

using System;
using System.ComponentModel;
using System.Web.UI;

public class MyControl : Control {
    private License license = null;
    public MyControl () {
        license = LicenseManager.Validate(typeof(MyControl), this);
    }
    public override void Dispose() {      
          if (license != null) {
            license.Dispose();
            license = null;
          }
      base.Dispose();
    }    
}
[Visual Basic]
Imports System
Imports System.ComponentModel
Imports System.Web.UI

<LicenseProvider(GetType(LicFileLicenseProvider))> Public Class  MyControl 
Inherits Control
    Private license As License
    
    Public Sub New()
        license = LicenseManager.Validate(GetType(MyControl), Me)
    End Sub
    
    Public Overrides Sub Dispose()
        If Not (license Is Nothing) Then
            license.Dispose()
            license = Nothing
        End If
    End Sub
End Class

The examples shown here use the built-in license provider class LicFileLicenseProvider, which enables the use of text license files and mimics the behavior of COM (ActiveX) licensing. More complex licensing scenarios (such as calling an XML Web service to limit the number of instances of a component) require different kinds of license providers.

A sample of licensing is provided in the Windows Forms QuickStart under Building Applications —> Creating Controls —> Licensing Controls.

See Also

Developing Components