授予组件和控件许可权限

来源:互联网 发布:照片说话软件 编辑:程序博客网 时间:2024/05/01 06:38
本随笔主要参考了MSDN

  在开发商业软件时,往往需要给软件实现某种类型的许可,以限制非授权用户的使用。一般情况下,开发者会采取建立并检查特定的授权文件或在注册表中添加表项的方法来实现授权机制。但对于商业控件的开发而言,它所面对的对象是二次开发者而不是最终用户,采用传统的方法进行授权验证会有不少的问题。令人欣喜的是,.NET框架提供了内置的授权方案,利用它能非常方便的实现带授权机制的控件开发,并且开发者可以覆盖它并创建自己的授权验证方案。

一、简单的一个例子

To enable licensing for your component or control

 

1、Apply a LicenseProviderAttribute to the class.

给类添加  LicenseProviderAttribute 特性。

2、Call Validate or IsValid in the constructor.

在构造函数中调用Validate函数。

3、Call Dispose on any granted license in the finalizer of the class or before the finalizer is called.

重写Dispose函数。

 

The following code examples use the built-in license provider class LicFileLicenseProvider, which enables you to use text license files.

 

using System;using System.ComponentModel;using System.Windows.Forms;namespace TestLicense{    // 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;                }            }        }    }          }
View Code

 

客户端代码如下:

namespace testIt{    class Program    {        static void Main(string[] args)        {            MyControl myControl = new MyControl();            Console.WriteLine(myControl.ToString());        }    }}
View Code

框架提供了许可提供程序类(LicFileLicenseProvider类),将其与自定义控件绑定在一起,即可通过LicenseManager.Validate共享方法在控件的构造函数中自动检查授权信息。默认情况下,系统会在控件文件MyControl.DLL所在目录下查找一个扩展名为lic的授权文件,其文件名是控件类的完整名称,例如:MyControl程序集的MyControl类的授权文件的名称应该是TestLicense.MyControl.lic。这个lic文件是一个文本文件,.NET规定其内容为如下形式的一串文本:
TestLicense.MyControl is a licensed component.
 如果找不到这个文件,或者文件的内容不符,MyControl控件在设计时和运行时都无法被使用,并引发一个异常。下图说明了这一过程 

 

二、实践一下,自己动手写一下

 显然,由于授权文件的内容是相对固定的,内置授权验证机制非常容易被破解,只需将lic文件复制到控件(NumericTextBox.dll)所在目录下即可,用户甚至可以毫不费力的自己建立一个lic文件。  通过建立自定义授权验证方案,可以很好地解决这一问题。自定义的许可提供程序类派生自LicenseProvider类,并重写其GetLicense虚拟方法,以实现自定义验证。同时将自定义许可提供程序类的名称作为参数传递给LicenseProvider特性。

主要是重写了读取证书文件内容和验证是否符合授权的方法。

namespace Test{    // Adds the LicenseProviderAttribute to the control.    [LicenseProvider(typeof(TukrinLicFileLicenseProvider))]    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;                }            }        }    }}

 

 LicWindowsFileLicensePrevider类的GetLicense方法由LicenseManager.Validate方法间接调用(在自定义控件的构造函数中自动执行),并可接收几个参数,用来确定当前指定控件的名称、当前是工作在设计时还是运行时,以及其他信息。若验证通过,GetLicense方法返回一个许可对象,正常运行;否则,抛出一个异常并返回Nothing,使自定义控件不可创建或不可使用。

namespace Test{    public class MyLicense : License    {        public override string LicenseKey        {            get            {                return "Tukrin";            }        }        public override void Dispose()        {        }    }    public class TukrinLicFileLicenseProvider : LicFileLicenseProvider    {        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)        {            if (IsKeyValid(GetKey(null), null))            {                return new MyLicense();            }            else            {                return null;            }        }        protected override string GetKey(Type type)        {            try            {                FileStream fs = File.OpenRead(Application.StartupPath + "\\license.lic");                StreamReader sr = new StreamReader(fs);                string all = sr.ReadToEnd();                return all;            }            catch (Exception)            {                return null;            }        }        protected override bool IsKeyValid(string key, Type type)        {            try            {                if (key.Contains("Tukrin"))                {                    return true;                }                else                {                    return false;                }            }            catch (Exception)            {                return false;            }        }    }}
View Code

 

 

好的参考链接:

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

LicFileLicenseProvider类

基于.NET架构带授权验证机制控件的开发

 

0 0
原创粉丝点击