C# Infralution Licensing System学习笔记(三)用户自定义控件Licensing应用

来源:互联网 发布:python算术运算符 编辑:程序博客网 时间:2024/05/22 02:21

1.创建一个UserControl,命名为UserControl1.

2.在UserControl1 class的声明前加入[LicenseProvider(typeof(Infralution.Licensing.EncryptedLicenseProvider))]

3.定义验证相关变量

4.在UserControl1的初始化函数添加验证代码。如下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Infralution.Licensing;
  9. namespace Geoff.Licensing
  10. {
  11.     [LicenseProvider(typeof(Infralution.Licensing.EncryptedLicenseProvider))]
  12.     public partial class UserControl1 : UserControl
  13.     {
  14.         const string LICENSE_PARAMETERS =
  15.             @"<LicenseParameters>
  16.                 <RSAKeyValue>
  17.                     <Modulus>rzRJ6dZ4ZTJ8/EzFtq2XIaik7QBSc24b8i8KJrPaxXGiCMDFcj8JtEFePa1SYX1W3cQoiwvCAF/MYmkXto74WdApYzILel+bOHSaUXTImrxpPtykNhPD7fF3PEXxrLRrTjUWQyEX5+XSaNPstmbDJw/Zg83mraCfmGeLcsiEypM=</Modulus>
  18.                     <Exponent>AQAB</Exponent>
  19.                 </RSAKeyValue>
  20.                 <DesignSignature>GJYsK2rQE7uj539+QCyJ3Z2GyrNEZQNxruxOQR6P2jo+Ze3Ev5Kgbf3N33RaLf7oEUsZNF4Gv7w5UFgYcdHzCphqznt4vcR++u9tb0sC5m6hf26538GXhkNodyVR9FDcv1AX4+PaM/kY+Z8mrMMmC148lYPpCHypXvgjOmDd9oQ=</DesignSignature>
  21.                 <RuntimeSignature>coZIUnOTaRZCIa67HBMM6/xbkQNYS+l4ROeLmzn7SqRamBwP7cbfOEAsxlFV2roKlY6oz1B91/PYcTVPLfWahwjHaiAaLjKAosXK41yZOozu0vRWpv4H9ERiEli0pYX0M8fMBIkvSDT6n9X9T/6U4ujyefv8b4IxbYsB3zsjjx0=</RuntimeSignature>
  22.                 <KeyStrength>7</KeyStrength>
  23.              </LicenseParameters>";
  24.         static LicenseContext lastDesignContext = null;
  25.         static bool licenseChecked = false;
  26.         public UserControl1()
  27.         {
  28.             InitializeComponent();
  29.             // 设定验证参数
  30.             EncryptedLicenseProvider.SetParameters(LICENSE_PARAMETERS);
  31.             if (LicenseManager.CurrentContext.UsageMode == LicenseUsageMode.Designtime)
  32.             {
  33.                 // 控件设计时检查验证
  34.                 if (!LicenseManager.IsLicensed(typeof(UserControl1)))
  35.                 {
  36.                     if (LicenseManager.CurrentContext != lastDesignContext)
  37.                     {
  38.                         LicenseInstallForm licenseForm = new LicenseInstallForm();
  39.                         licenseForm.ShowDialog("UserControl1""www.geoffhong.com"typeof(UserControl1));
  40.                         lastDesignContext = LicenseManager.CurrentContext;
  41.                     }
  42.                 }
  43.             }
  44.             else
  45.             {
  46.                 // 运行时只检查控件有没有授权
  47.                 if (!licenseChecked)
  48.                 {
  49.                     if (!LicenseManager.IsLicensed(typeof(UserControl1)))
  50.                     {
  51.                         MessageBox.Show("程式使用未授权的UserControl1""程式未授权");
  52.                     }
  53.                 }
  54.             }
  55.             licenseChecked = true; ;
  56.         }
  57.     }
  58. }

5.在引用UserControl1的窗体,新增加入UserControl1控件,会出现一个输入注册码的窗体,与Windows程式验证一致。

原创粉丝点击