如何:授予组件和控件许可权限

.NET Framework 提供了一个对所有组件和控件(包括 Windows 窗体控件和 ASP.NET 服务器控件)都相同的授权模型,它与 Microsoft ActiveX® 控件的授权完全兼容。

通过授权,您作为组件或控件作者,可以验证开发人员是否被授予使用您的组件或控件的权限,来保护自己的知识产权。 在设计时(这时开发人员将您的组件或控件合并到应用程序中)进行该验证比在运行时进行验证更为重要。 如果开发人员在设计时合法使用经您授权的组件或控件,则开发人员的应用程序将获得一个运行时许可证,并可以随意分发该许可证。

借助于授权模型,您可以拥有很多其他级别的授权支持。 该模型可以将验证逻辑与组件或控件分隔开。 许可证提供程序授予许可证并执行验证逻辑。 提供程序是从 LicenseProvider 派生出来的类。 启用授权所必须执行的步骤非常简单。

使用 LicFileLicenseProvider 提供的 LicenseProvider 的默认实现时,将按以下方式设置许可证文件的格式:

  • 文件的名称必须是文件扩展名为 .LIC 的类的完全限定名,包括命名空间。 例如:

    Namespace1.Class1.LIC

  • 许可证文件的内容应包含以下文本字符串:

    “myClassName 是一个授权组件。”

    myClassName 是类的完全限定名。 例如:

    “Namespace1.Class1 是一个授权组件。”

下面的代码示例演示 Windows 窗体控件和 ASP.NET 服务器控件实现授权的简单情况。

启用您的组件或控件的授权

  1. LicenseProviderAttribute 应用于类。

  2. 在构造函数中调用 ValidateIsValid

  3. 在类的终结器中或在调用终结器之前对任何已授予的许可证调用 Dispose

下面的代码示例使用了内置的许可证提供程序类 LicFileLicenseProvider,您可以通过它使用文本许可证文件并模仿 COM (ActiveX) 授权的行为。 更加复杂的授权情况(例如调用 XML Web services 来限制组件实例的数目)需要使用多种不同的许可证提供程序。

示例

Imports System
Imports System.ComponentModel
Imports System.Windows.Forms

' Adds the LicenseProviderAttribute to the control.
<LicenseProvider(GetType(LicFileLicenseProvider))> _
Public Class MyControl
    Inherits Control

    ' Creates a new, null license. 
    Private license As License = Nothing     

    Public Sub New()        

        ' Adds Validate to the control's constructor.
        license = LicenseManager.Validate(GetType(MyControl), Me)

        ' Insert code to perform other instance creation tasks here. 

    End Sub 

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then 
            If (license IsNot Nothing) Then
                license.Dispose()
                license = Nothing 
            End If 
        End If 

    End Sub     

End Class
using System;
using System.ComponentModel;
using System.Windows.Forms;


// Adds the LicenseProviderAttribute to the control.
[LicenseProvider(typeof(LicFileLicenseProvider))]
public class MyControl : Control 
{

   // Creates a new, null license. 
   private License license = null;

   public MyControl () 
   {

      // Adds Validate to the control's constructor.
      license = LicenseManager.Validate(typeof(MyControl), this);

      // Insert code to perform other instance creation tasks here.
   }

   protected override void Dispose(bool disposing) 
   {
      if(disposing)
      {
         if (license != null) 
         {
            license.Dispose();
            license = null;
         }
      }
   }

}
// Adds the LicenseProviderAttribute to the control.

[LicenseProvider(LicFileLicenseProvider::typeid)]
public ref class MyControl: public Control
{
   // Creates a new, null license. 
private:
   License^ license;

public:
   MyControl()
   {

      // Adds Validate to the control's constructor.
      license = LicenseManager::Validate( MyControl::typeid, this );

      // Insert code to perform other instance creation tasks here.
   }

public:
   ~MyControl()
   {
      if ( license != nullptr )
      {
         delete license;
         license = nullptr;
      }
   }
};
Imports System
Imports System.ComponentModel
Imports System.Web.UI

' Adds the LicenseProviderAttribute to the control.
<LicenseProvider(GetType(LicFileLicenseProvider))> Public Class MyControl
    Inherits Control

    ' Creates a new, null license. 
    Private license As License

    Public Sub New()

        ' Adds Validate to the control's constructor.
        license = LicenseManager.Validate(GetType(MyControl), Me)

        ' Insert code to perform other instance creation tasks here. 

    End Sub 

    Public Overrides Sub Dispose()
        If (license IsNot Nothing) Then
            license.Dispose()
            license = Nothing 
        End If 
        MyBase.Dispose()
    End Sub 
End Class
using System;
using System.ComponentModel;
using System.Web.UI;

// Adds the LicenseProviderAttribute to the control. 
public class MyServerControl : Control 
{
    // Creates a new, null license. 
    private License license = null;

    public MyServerControl() 
    {
        // Adds Validate to the control's constructor.
        license = LicenseManager.Validate(typeof(MyServerControl), this);

        // Insert code to perform other instance creation tasks here.
    }

    public override void Dispose() 
    {      
        if (license != null) 
        {
            license.Dispose();
            license = null;
        }

        base.Dispose();
    }    
}

请参见

参考

LicenseProviderAttribute

LicenseProvider

其他资源

组件创作