首页 > 代码库 > 授予组件和控件许可权限

授予组件和控件许可权限

本随笔主要参考了MSDN

一、简单的一个例子

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

 

  程序执行目录下如果有TestLicense.MyControl.lic 文件。且内容为:

TestLicense.MyControl is a licensed component.

则,该组件可以正常使用。

 

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

 

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

技术分享
namespace Test
{
    // 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 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类

 

 

授予组件和控件许可权限