使用OpenLicense来管理应用程序许可

通常对于商业软件来说都会给自己的软件产品添加使用限制,如时间长度、使用次数等

许可中包括了使用限制的各种信息,包括:
1、时间长度
2、使用次数
3、版本升级
4、在线注册
这几天在http://www.spextreme.com/网站看到其开源的OpenLicense。下载下来试用了一下。觉得不错。
它的主要特点:图形化界面创建许可,支持产品许可,512位加密,许可钥匙,各种许可限制及设计/运行的支持.
这里下载Open License

这里我讲一下最简单的设置许可的方法。

使用VS建立一个Windows工程LicenseDemo,默认里面有一个Form1.先将其编译一次。然后使用下载下来的图形化界面许可创建器来建立许可文件OpenLicenseBuilder.exe .

这里添加刚才编译的LicenseDemo.exe

这样就添加一个Beta Constraint.然后在属性窗口中就可以设置其许可的限制

做完之后将其保存起来.取名规则为namespace.className.lic 这里我取为LicenseDemo.Form1.lic ,将此文件添加到VS工程中.

在Form1中我们要对许可进行检验:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using OpenLicense;
namespace LicenseDemo
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 [LicenseProvider( typeof( OpenLicense.OpenLicenseProvider ) )]
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private License license = null;
  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   if(LicenseManager.IsValid( typeof( Form1 ), this, out license ))
   {
  
   
    if( license == null )
    {
     throw new System.ApplicationException( "A sutable license file couldn't be found" );
    }
    else if( ((OpenLicenseFile)license).FailureReason != String.Empty )
    {
     throw new System.ApplicationException( ((OpenLicenseFile)license).FailureReason );
    }

   
   }
   else
   {
    MessageBox.Show("没有正确的许可");
   }
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.Size = new System.Drawing.Size(300,300);
   this.Text = "Form1";
  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
 }
}

同时注意许可文件(lic)是可以加密码的,上面的许可文件我没有进行加密,如果加密码的话,要在 AssemblyInfo.cs文件中加上一行代码,告诉解密码KEY. [assembly: OpenLicense.AssemblyOpenLicenseKey( "xxxxxx" )]

对于LIC文件可以作为内嵌资源来使用.但在我正式运行时,我发觉对于执行路径必须有许可文件的存在.(如果运行程序需要将上面生成的许可文件复制到BIN目录下.).

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值