使用OpenLicense來管理應用程式許可

來源:互聯網
上載者:User

通常對於商業軟體來說都會給自己的軟體產品添加使用限制,如時間長度、使用次數等

許可中包括了使用限制的各種資訊,包括:
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目錄下.).

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.