NET许可证及License

有时,我们需要为类或组件等添加许可。

而NET FCL为   我们提供了一些相关类的使用。

这些类都在System.ComponentModel命名空间下。

下面是简单的一个实现:

 

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Linq;
None.gif
using  System.Text;
None.gif
using  Microsoft.Win32;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.ComponentModel;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif * Test by McJeremy&Xu
InBlock.gif * url:
http://www.cnblogs.com/mcjeremy
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
namespace  MyLicenseTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     * License许可由License(许可证)、LicenseProvider(许可证提供器)、LicenseManager管理三部分组成
InBlock.gif     * 使用过程一般如下:
InBlock.gif     * 1、创建继承自License并实现其抽象属性LicenseKey的License类
InBlock.gif     * 2、创建继承自LicenseProvider并实现其抽象方法GetLicense的Provider类
InBlock.gif     * 3、在需要验证许可的类或组件(等)的构造方法中使用LicenseMangager的静态Validate或IsValid方法,其中
InBlock.gif     * Validate产生异常,而IsValid不产生
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MyLicenseTestClass mltc 
= new MyLicenseTestClass();
InBlock.gif                Console.WriteLine(
"MyLicenseTestClass被许可使用了!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (LicenseException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(e.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//Step1:实现License类
InBlock.gif
    public class SimpleRuntimeLicense : License
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string TypeCode;
InBlock.gif        
public override string LicenseKey
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn TypeCode; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public SimpleRuntimeLicense(Type type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.TypeCode = type.GUID.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//Step2:实现licenseProvider类,使用Provider设计模式
InBlock.gif    
//NET提供了LicFileLicenseProvider类,一般情况下,我们需要提供我们自己的Provider来实现Validate逻辑
InBlock.gif
    public class SimpleRuntimeLicenseProvider : LicenseProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="context">验证上下文    </param>
InBlock.gif        
/// <param name="type">被验证对象类型</param>
InBlock.gif        
/// <param name="instance">被验证对象实例</param>
InBlock.gif        
/// <param name="allowExceptions">是否在验证没通过时抛出异常</param>
ExpandedSubBlockEnd.gif        
/// <returns>验证通过后的许可证</returns>

InBlock.gif        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//通过licenseUsageMode可以指定验证范围是在设计时还是运行时
InBlock.gif
            if (context.UsageMode == LicenseUsageMode.Runtime || context.UsageMode == LicenseUsageMode.Runtime)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RegistryKey rk 
= Registry.LocalMachine.OpenSubKey(@"SOFTWARE\NETLicenseTest");
InBlock.gif                
if (null != rk)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string lick = rk.GetValue("SN").ToString();
InBlock.gif                        
if (!string.IsNullOrEmpty(lick))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (string.Compare(lick, type.GUID.ToString(),true== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
return new SimpleRuntimeLicense(type);
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                    
catch dot.gif{
InBlock.gif                        
//设定没有许可证时的提供信息
InBlock.gif
                        throw new LicenseException(type, instance, "您没有许可证!无法运行!");
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//Step3:在需要许可验证的类型中使用LicenseProvider和LicenseManager    
InBlock.gif
    [Guid("7F46DB6D-98CD-4cb7-BA95-014F678B2375")]
InBlock.gif    [LicenseProvider(
typeof(SimpleRuntimeLicenseProvider))]   
InBlock.gif    
public class MyLicenseTestClass:IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
InBlock.gif        License lic 
= null;
InBlock.gif        
public MyLicenseTestClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lic
=LicenseManager.Validate(this.GetType(), this);            
InBlock.gif            
//LicenseManager.IsValid(
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IDisposable 成员#region IDisposable 成员
InBlock.gif        
private bool disposed = false;
InBlock.gif
InBlock.gif        
void IDisposable.Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dispose(
true);
InBlock.gif            GC.SuppressFinalize(
this);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (null != lic)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        lic.Dispose();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
~MyLicenseTestClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dispose(
false);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/McJeremy/archive/2009/04/10/1432913.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值