1.创建一个UserControl,命名为UserControl1.
2.在UserControl1 class的声明前加入[LicenseProvider(typeof(Infralution.Licensing.EncryptedLicenseProvider))]
3.定义验证相关变量
4.在UserControl1的初始化函数添加验证代码。如下
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- using Infralution.Licensing;
- namespace Geoff.Licensing
- {
- [LicenseProvider(typeof(Infralution.Licensing.EncryptedLicenseProvider))]
- public partial class UserControl1 : UserControl
- {
- const string LICENSE_PARAMETERS =
- @"<LicenseParameters>
- <RSAKeyValue>
- <Modulus>rzRJ6dZ4ZTJ8/EzFtq2XIaik7QBSc24b8i8KJrPaxXGiCMDFcj8JtEFePa1SYX1W3cQoiwvCAF/MYmkXto74WdApYzILel+bOHSaUXTImrxpPtykNhPD7fF3PEXxrLRrTjUWQyEX5+XSaNPstmbDJw/Zg83mraCfmGeLcsiEypM=</Modulus>
- <Exponent>AQAB</Exponent>
- </RSAKeyValue>
- <DesignSignature>GJYsK2rQE7uj539+QCyJ3Z2GyrNEZQNxruxOQR6P2jo+Ze3Ev5Kgbf3N33RaLf7oEUsZNF4Gv7w5UFgYcdHzCphqznt4vcR++u9tb0sC5m6hf26538GXhkNodyVR9FDcv1AX4+PaM/kY+Z8mrMMmC148lYPpCHypXvgjOmDd9oQ=</DesignSignature>
- <RuntimeSignature>coZIUnOTaRZCIa67HBMM6/xbkQNYS+l4ROeLmzn7SqRamBwP7cbfOEAsxlFV2roKlY6oz1B91/PYcTVPLfWahwjHaiAaLjKAosXK41yZOozu0vRWpv4H9ERiEli0pYX0M8fMBIkvSDT6n9X9T/6U4ujyefv8b4IxbYsB3zsjjx0=</RuntimeSignature>
- <KeyStrength>7</KeyStrength>
- </LicenseParameters>";
- static LicenseContext lastDesignContext = null;
- static bool licenseChecked = false;
- public UserControl1()
- {
- InitializeComponent();
- // 设定验证参数
- EncryptedLicenseProvider.SetParameters(LICENSE_PARAMETERS);
- if (LicenseManager.CurrentContext.UsageMode == LicenseUsageMode.Designtime)
- {
- // 控件设计时检查验证
- if (!LicenseManager.IsLicensed(typeof(UserControl1)))
- {
- if (LicenseManager.CurrentContext != lastDesignContext)
- {
- LicenseInstallForm licenseForm = new LicenseInstallForm();
- licenseForm.ShowDialog("UserControl1", "www.geoffhong.com", typeof(UserControl1));
- lastDesignContext = LicenseManager.CurrentContext;
- }
- }
- }
- else
- {
- // 运行时只检查控件有没有授权
- if (!licenseChecked)
- {
- if (!LicenseManager.IsLicensed(typeof(UserControl1)))
- {
- MessageBox.Show("程式使用未授权的UserControl1", "程式未授权");
- }
- }
- }
- licenseChecked = true; ;
- }
- }
- }
5.在引用UserControl1的窗体,新增加入UserControl1控件,会出现一个输入注册码的窗体,与Windows程式验证一致。