zoukankan
html css js c++ java
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
}
}
<h3>
心静似高山流水不动,心清若巫峰雾气不沾。
</h3>
查看全文
相关阅读:
通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[中]:管道如何处理请求
通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[上]:采用管道处理请求
.NET Core的文件系统[5]:扩展文件系统构建一个简易版“云盘”
[WCF]缺少一行代码引发的血案
如何利用ETW(Event Tracing for Windows)记录日志
.NET Core的日志[5]:利用TraceSource写日志
.NET Core的日志[4]:将日志写入EventLog
.NET Core的日志[3]:将日志写入Debug窗口
.NET Core的日志[2]:将日志输出到控制台
.NET Core采用的全新配置系统[10]: 配置的同步机制是如何实现的?
原文地址:https://www.cnblogs.com/McJeremy/p/1432913.html
最新文章
每周.NET前沿技术文章摘要(2017-06-21)
Spring Data JPA使用keywords关键字实现CAST函数
CentOS以守护进程的方式启动程序的另类用法daemon
Windows 10原版ISO下载地址(持续更新)
Windows 10官方原版ISO制作方法
使用UltraISO制作Windows 10启动U盘
狗日的系统之家下载的Windows 10 1803/1809系统不干净,捆绑自动安装腾讯关键等软件
Linux/CentOS设置全局代理(http)
brew安装sshpass
Python包管理工具pip安装
热门文章
Python生成requirements.txt包依赖管理文件
VSCode换行符
CentOS下使用autoenv实现进入特定目录后运行特定环境变量
Spring Data JPA使用getOne方法报错:Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate
群晖NAS百度云Docker客户端下载目录没有权限的问题解决
群晖NAS的Docker容器使用中国镜像加速
群晖NAS使用Docker安装迅雷离线下载出现the active key is not valid.
使用Docker中国官方镜像的加速地址
黑群晖NAS安装方法(收集)/物理机/VMware虚拟机/KVM虚拟机(转)
群晖NAS简介(转)
Copyright © 2011-2022 走看看