.NET Framework 提供了一个对所有组件和控件(包括 Windows 窗体控件和 ASP.NET 服务器控件)都相同的授权模型,它与 Microsoft ActiveX® 控件的授权完全兼容。通过授权,您作为组件或控件作者,可以验证开发人员是否被 授予使用您的组件或控件的权限,来保护自己的知识产权。在设计时(这时开发人员将您的组件或控件合并到应用程序中)进行该验证比在运行时进行验证更为重要。如果开发人员在设计时合法使用经您授权的组件或控件,则开发人员的应用程序将获得一个运行时许可证,并可以随意分发该许可证。借助于授权模型,您可以拥有很多其他级别的授权支持。该模型可以将验证逻辑与组件或控件分隔开。许可证提供程序授予许可证并执行验证逻辑。提供程序是从 System.ComponentModel.LicenseProvider 派生出来的类。启用授权所必须执行的步骤非常简单。使用 LicFileLicenseProvider 提供的 LicenseProvider 的默认实现时,将按以下方式设置许可证文件的格式:文件的名称必须是文件扩展名为 .LIC 的类的完全限定名,包括命名空间。例如:Namespace1.Class1.LIC许可证文件的内容应包含以下文本字符串:"myClassName 是一个授权组件。”myClassName 是类的完全限定名。例如:“Namespace1.Class1 是一个授权组件。”下面的代码示例演示 Windows 窗体控件和 ASP.NET 服务器控件实现授权的简单情况。启用您的组件或控件的授权 将 LicenseProviderAttribute 应用于类。在构造函数中调用 Validate 或 IsValid。在类的终结器中或在调用终结器之前对任何已授予的许可证调用 Dispose。下面的代码示例使用了内置的许可证提供程序类 LicFileLicenseProvider,您可以通过它使用文本许可证文件并模仿 COM (ActiveX) 授权的行为。更加复杂的授权情况(例如调用 XML Web services 来限制组件实例的数目)需要使用多种不同的许可证提供程序。示例 Visual Basic 复制代码 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 Not (license Is Nothing) Then license.Dispose() license = Nothing End If End If End Sub
End Class
C# 复制代码 using System; using System.ComponentModel; using System.Windows.Forms; // Adds the LicenseProviderAttribute to the control. [LicenseProvider(typeof(LicFileLicenseProvider))] public class MyControl : Control {
} C++ 复制代码 // 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; } } }; J# 复制代码 import System.*; import System.ComponentModel.*; import System.Windows.Forms.*; // Adds the LicenseProviderAttribute to the control. /** @attribute LicenseProvider(LicFileLicenseProvider.class) */ public class MyControl extends Control { // Creates a new, null license. private License license = null; public MyControl() { // Adds Validate to the control's constructor. license = LicenseManager.Validate(MyControl.class.ToType(), this); // Insert code to perform other instance creation tasks here. }
protected void Dispose(boolean disposing) { if (disposing) { if (license != null) { license.Dispose(); license = null; } } } } Visual Basic 复制代码 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 Not (license Is Nothing) Then license.Dispose() license = Nothing End If MyBase.Dispose() End Sub End Class
C# 复制代码 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 应用于类。在构造函数中调用 Validate 或 IsValid。在类的终结器中或在调用终结器之前对任何已授予的许可证调用 Dispose。下面的代码示例使用了内置的许可证提供程序类 LicFileLicenseProvider,您可以通过它使用文本许可证文件并模仿 COM (ActiveX) 授权的行为。更加复杂的授权情况(例如调用 XML Web services 来限制组件实例的数目)需要使用多种不同的许可证提供程序。示例
Visual Basic 复制代码
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 Not (license Is Nothing) Then
license.Dispose()
license = Nothing
End If
End If End Sub
End Class
C# 复制代码
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;
}
}
}
}
C++ 复制代码
// 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;
}
}
};
J# 复制代码
import System.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
// Adds the LicenseProviderAttribute to the control.
/** @attribute LicenseProvider(LicFileLicenseProvider.class)
*/
public class MyControl extends Control
{
// Creates a new, null license.
private License license = null; public MyControl()
{
// Adds Validate to the control's constructor.
license = LicenseManager.Validate(MyControl.class.ToType(), this); // Insert code to perform other instance creation tasks here.
}
protected void Dispose(boolean disposing)
{
if (disposing) {
if (license != null) {
license.Dispose();
license = null;
}
}
}
}
Visual Basic 复制代码
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 Not (license Is Nothing) Then
license.Dispose()
license = Nothing
End If
MyBase.Dispose()
End Sub
End Class
C# 复制代码
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();
}
}
如果你开发的是windows应用程序 那么就要using 命名控件
如果你开发的是web网页 就像这样 <%@ Import Namespace ="System.Data" %> 引入
using System.Security.Permissions;
public class Class1
{
//可以被任何人调用
public void A()
{
}
//只能被具有指定公匙的assembly的类调用
[StrongNameIdentityPermissionAttribute(SecurityAction.LinkDemand, PublicKey="0x00240000048...653F1F8A1C9")> _
public void A2()
{
}
} 2、通过发放License来控制
下面这个里面有一些示例项目,可以找来看看
http://www.spextreme.com/osp/open_license/