NET许可证及License
生活随笔
收集整理的這篇文章主要介紹了
NET许可证及License
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有時,我們需要為類或組件等添加許可。
而NET FCL為???我們提供了一些相關類的使用。
這些類都在System.ComponentModel命名空間下。
下面是簡單的一個實現:
?
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?Microsoft.Win32;
using?System.Runtime.InteropServices;
using?System.ComponentModel;
/**//*
?*?Test?by?McJeremy&Xu
?*?url:http://www.cnblogs.com/mcjeremy
?*?
?*/
namespace?MyLicenseTest
{
????/**//*
?????*?License許可由License(許可證)、LicenseProvider(許可證提供器)、LicenseManager管理三部分組成
?????*?使用過程一般如下:
?????*?1、創建繼承自License并實現其抽象屬性LicenseKey的License類
?????*?2、創建繼承自LicenseProvider并實現其抽象方法GetLicense的Provider類
?????*?3、在需要驗證許可的類或組件(等)的構造方法中使用LicenseMangager的靜態Validate或IsValid方法,其中
?????*?Validate產生異常,而IsValid不產生
?????*/
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????try
????????????{
????????????????MyLicenseTestClass?mltc?=?new?MyLicenseTestClass();
????????????????Console.WriteLine("MyLicenseTestClass被許可使用了!");
????????????}
????????????catch?(LicenseException?e)
????????????{
????????????????Console.WriteLine(e.Message);
????????????}
????????????Console.ReadLine();
????????}
????}
????//Step1:實現License類
????public?class?SimpleRuntimeLicense?:?License
????{
????????private?string?TypeCode;
????????public?override?string?LicenseKey
????????{
????????????get?{?return?TypeCode;?}
????????}
????????public?override?void?Dispose()
????????{
????????}
????????public?SimpleRuntimeLicense(Type?type)
????????{
????????????this.TypeCode?=?type.GUID.ToString();
????????}
????}
????//Step2:實現licenseProvider類,使用Provider設計模式
????//NET提供了LicFileLicenseProvider類,一般情況下,我們需要提供我們自己的Provider來實現Validate邏輯
????public?class?SimpleRuntimeLicenseProvider?:?LicenseProvider
????{
????????/**////?<summary>
????????///?
????????///?</summary>
????????///?<param?name="context">驗證上下文????</param>
????????///?<param?name="type">被驗證對象類型</param>
????????///?<param?name="instance">被驗證對象實例</param>
????????///?<param?name="allowExceptions">是否在驗證沒通過時拋出異常</param>
????????///?<returns>驗證通過后的許可證</returns>
????????public?override?License?GetLicense(LicenseContext?context,?Type?type,?object?instance,?bool?allowExceptions)
????????{
????????????//通過licenseUsageMode可以指定驗證范圍是在設計時還是運行時
????????????if?(context.UsageMode?==?LicenseUsageMode.Runtime?||?context.UsageMode?==?LicenseUsageMode.Runtime)
????????????{
????????????????RegistryKey?rk?=?Registry.LocalMachine.OpenSubKey(@"SOFTWARE\NETLicenseTest");
????????????????if?(null?!=?rk)
????????????????{
????????????????????try
????????????????????{
????????????????????????string?lick?=?rk.GetValue("SN").ToString();
????????????????????????if?(!string.IsNullOrEmpty(lick))
????????????????????????{
????????????????????????????if?(string.Compare(lick,?type.GUID.ToString(),true)?==?0)
????????????????????????????{
????????????????????????????????return?new?SimpleRuntimeLicense(type);
????????????????????????????}
????????????????????????}
????????????????????}
????????????????????catch?{
????????????????????????//設定沒有許可證時的提供信息
????????????????????????throw?new?LicenseException(type,?instance,?"您沒有許可證!無法運行!");
????????????????????}
????????????????}
????????????}
????????????return?null;
????????}
????}
????//Step3:在需要許可驗證的類型中使用LicenseProvider和LicenseManager????
????[Guid("7F46DB6D-98CD-4cb7-BA95-014F678B2375")]
????[LicenseProvider(typeof(SimpleRuntimeLicenseProvider))]???
????public?class?MyLicenseTestClass:IDisposable
????{
???????
????????License?lic?=?null;
????????public?MyLicenseTestClass()
????????{
????????????lic=LicenseManager.Validate(this.GetType(),?this);????????????
????????????//LicenseManager.IsValid(
????????}
????????IDisposable?成員#region?IDisposable?成員
????????private?bool?disposed?=?false;
????????void?IDisposable.Dispose()
????????{
????????????Dispose(true);
????????????GC.SuppressFinalize(this);
????????}
????????public?void?Dispose(bool?disposing)
????????{
????????????if?(!disposed)
????????????{
????????????????if?(disposing)
????????????????{
????????????????????if?(null?!=?lic)
????????????????????{
????????????????????????lic.Dispose();
????????????????????}
????????????????}
????????????}
????????}
????????~MyLicenseTestClass()
????????{
????????????Dispose(false);
????????}
????????#endregion
????}
}
轉載于:https://www.cnblogs.com/McJeremy/archive/2009/04/10/1432913.html
總結
以上是生活随笔為你收集整理的NET许可证及License的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven处理和java平级的资源文件
- 下一篇: JEECG-V3 版本相关文档开放通知