首页 > 代码库 > 授予组件和控件许可权限
授予组件和控件许可权限
本随笔主要参考了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; } } } } }
客户端代码如下:
namespace testIt { class Program { static void Main(string[] args) { MyControl myControl = new MyControl(); Console.WriteLine(myControl.ToString()); } } }
程序执行目录下如果有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; } } } } }
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; } } } }
好的参考链接:
如何:授予组件和控件许可权限
LicFileLicenseProvider类
授予组件和控件许可权限
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。