佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析

深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析

熱門標(biāo)簽:科大訊飛語音識別系統(tǒng) 網(wǎng)站排名優(yōu)化 電銷業(yè)務(wù) 客戶服務(wù) 百度AI接口 電商新玩法 國美全國運(yùn)營中心 人工智能

在使用第三方類庫時,經(jīng)常會看到它自帶的演示程序中,包含有這樣的Demo許可文件

復(fù)制代碼 代碼如下:

Infragistics.Win.Misc.UltraButton, Infragistics2.Win.Misc.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31
Infragistics.Win.Misc.UltraLabel, Infragistics2.Win.Misc.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31
Infragistics.Win.Printing.UltraPrintPreviewDialog, Infragistics2.Win.UltraWinPrintPreviewDialog.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31
Infragistics.Win.UltraWinDataSource.UltraDataSource, Infragistics2.Win.UltraWinDataSource.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31

這個文件的格式是文本文件,但要按照它的格式要求來寫:

控件名稱, 程序集全名稱

首先根據(jù)需要,寫一個需要被授權(quán)的控件列表,格式如上所示。例如,HostApp.exe 的應(yīng)用程序要引用Samples.DLL 中的授權(quán)控件 MyCompany.Samples.LicControl1,則可以創(chuàng)建包含以下內(nèi)容的 HostAppLic.txt。 MyCompany.Samples.LicControl1, Samples.DLL。

再調(diào)用下面的命令創(chuàng)建名為 HostApp.exe.licenses 的 .licenses 文件。 lc /target:HostApp.exe /complist:hostapplic.txt /i:Samples.DLL /outdir:c:\bindir

生成將 .licenses 文件作為資源嵌入在HostApp.exe的資源中。如果生成的是 C# 應(yīng)用程序,則應(yīng)使用下面的命令生成應(yīng)用程序。

csc /res:HostApp.exe.licenses /out:HostApp.exe *.cs

.NET Framework SDK目錄中的LC.EXE文件是由.NET語言編寫的,它的功能就是為了根據(jù)許可文件的內(nèi)容,生成資源文件。在編譯的最后時刻,由CSC編譯器把生成的資源文件嵌入到執(zhí)行文件中。

用.NET Reflector載入LC.EXE,開始源代碼分析之旅。



程序的入口處先是分析命令行參數(shù),根據(jù)參數(shù)的不同來執(zhí)行指定的功能。先看一個完整的參數(shù)列表。代碼是下面三行
復(fù)制代碼 代碼如下:

if (!ProcessArgs(args))
 {
     return num;
 }



MSDN有完整的解釋,拷貝到下面方便您參考,以減少因查找MSDN引起思路中斷。
/complist:filename   指定包含授權(quán)組件列表的文件名,這些授權(quán)組件要包括到 .licenses 文件中。每個組件用它的全名引用,并且每行只有一個組件。命令行用戶可為項目中的每個窗體指定一個單獨(dú)的文件。Lc.exe 接受多個輸入文件并產(chǎn)生一個 .licenses 文件。
/h[elp]     顯示該工具的命令語法和選項。
/i:module   指定模塊,這些模塊包含文件 /complist 中列出的組件。若要指定多個模塊,請使用多個 /i 標(biāo)志。
/nologo  取消顯示 Microsoft 啟動標(biāo)題。
/outdir:path  指定用來放置輸出 .licenses 文件的目錄。
/target:targetPE   指定為其生成 .licenses 文件的可執(zhí)行文件。
/v   指定詳細(xì)模式;顯示編譯進(jìn)度信息。
/?  顯示該工具的命令語法和選項。
ProcessArgs方法的關(guān)鍵作用是分析出組件列表,程序集列表,如下面的代碼所示
復(fù)制代碼 代碼如下:

  if ((!flag3 (str2.Length > 7)) str2.Substring(0, 7).ToUpper(CultureInfo.InvariantCulture).Equals("TARGET:"))
 {
       targetPE = str2.Substring(7);
       flag3 = true;
 }
if ((!flag3 (str2.Length > 8)) str2.Substring(0, 9).ToUpper(CultureInfo.InvariantCulture).Equals("COMPLIST:"))
 {
      string str3 = str2.Substring(9);
      if ((str3 != null) (str3.Length > 1))
        {
                    if (compLists == null)
                    {
                        compLists = new ArrayList();
                    }
                    compLists.Add(str3);
                    flag3 = true;
       }
}
if ((!flag3 (str2.Length > 2)) str2.Substring(0, 2).ToUpper(CultureInfo.InvariantCulture).Equals("I:"))
 {
       string str4 = str2.Substring(2);
       if (str4.Length > 0)
        {
                    if (assemblies == null)
                    {
                        assemblies = new ArrayList();
                    }
                    assemblies.Add(str4);
        }
        flag3 = true;
}

分析出組件和程序集之后,再來ResolveEventHandler 委托的含義。如果運(yùn)行庫類加載程序無法解析對程序集、類型或資源的引用,則將引發(fā)相應(yīng)的事件,從而使回調(diào)有機(jī)會通知運(yùn)行庫引用的程序集、類型或資源位于哪個程序集中。ResolveEventHandler 負(fù)責(zé)返回解析類型、程序集或資源的程序集。
復(fù)制代碼 代碼如下:

ResolveEventHandler handler = new ResolveEventHandler(LicenseCompiler.OnAssemblyResolve);
AppDomain.CurrentDomain.AssemblyResolve += handler;

對第一部參數(shù)分析出來的組件列表,依次循環(huán),為它們產(chǎn)生授權(quán)許可
復(fù)制代碼 代碼如下:

DesigntimeLicenseContext creationContext = new DesigntimeLicenseContext();
foreach (string str in compLists)
{
   key = reader.ReadLine();    hashtable[key] = Type.GetType(key);        LicenseManager.CreateWithContext((Type) hashtable[key], creationContext);
}

最后,生成許可文件并保存到磁盤中,等待CSC編譯器將它編譯成資源文件,嵌入到程序集中。
復(fù)制代碼 代碼如下:

string path = null;
if (outputDir != null)
 {
    path = outputDir + @"\" + targetPE.ToLower(CultureInfo.InvariantCulture) + ".licenses";
 }
else
 {
      path = targetPE.ToLower(CultureInfo.InvariantCulture) + ".licenses";
 }
 Stream o = null;
 try
     {
            o = File.Create(path);
           DesigntimeLicenseContextSerializer.Serialize(o, targetPE.ToUpper(CultureInfo.InvariantCulture), creationContext);
     }
     finally
     {
            if (o != null)
            {
                o.Flush();
                o.Close();
            }
     }

這種方式是.NET Framework推薦的保護(hù)組件的方式,與我們平時所討論的輸入序列號,RSA簽名不同。
來看一下,商業(yè)的組件是如何應(yīng)用這種技術(shù)保護(hù)組件的。
復(fù)制代碼 代碼如下:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace ComponentArt.Licensing.Providers
{
  #region RedistributableLicenseProvider
    public class RedistributableLicenseProvider : System.ComponentModel.LicenseProvider
    {
    const string strAppKey = "This edition of ComponentArt Web.UI is licensed for XYZ application only.";   

    public override System.ComponentModel.License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
    {
      if (context.UsageMode == LicenseUsageMode.Designtime)
      {
        // We are not going to worry about design time Issue a license
        return new ComponentArt.Licensing.Providers.RedistributableLicense(this, "The App");
      }
      else
      {
        string strFoundAppKey;
        // During runtime, we only want this control to run in the application
        // that it was packaged with.
        HttpContext ctx = HttpContext.Current;
        strFoundAppKey = (string)ctx.Application["ComponentArtWebUI_AppKey"];
        if(strAppKey == strFoundAppKey)
          return new ComponentArt.Licensing.Providers.RedistributableLicense(this, "The App");
        else
          return null;
      }
    }
  }
  #endregion
  #region RedistributableLicense Class
  public class RedistributableLicense : System.ComponentModel.License
  {
    private ComponentArt.Licensing.Providers.RedistributableLicenseProvider owner;
    private string key;
    public RedistributableLicense(ComponentArt.Licensing.Providers.RedistributableLicenseProvider owner, string key)
    {
      this.owner = owner;
      this.key = key;
    }
    public override string LicenseKey
    {
      get
      {
        return key;
      }
    }
    public override void Dispose()
    {
    }
  }
  #endregion
}

首先要創(chuàng)建一個類型,繼承于License類型,再創(chuàng)建一個繼承于LicenseProvider的類型,用于頒發(fā)許可證,包含在設(shè)計時許可和運(yùn)行時許可,從上面的例子中可以看到,設(shè)計時沒有限制,可以運(yùn)行,但是到運(yùn)行時,你必須有序列號,它才會生成許可對象,而不是返回null給.NET Framework類型。整個驗證過程由.NET完成。
你只需要像下面這樣,應(yīng)用這個許可保護(hù)機(jī)制:
復(fù)制代碼 代碼如下:

[LicenseProvider(typeof(RedistributableLicenseProvider))]
public class MyControl : Control {
    // Insert code here.
    protected override void Dispose(bool disposing) {
       /* All components must dispose of the licenses they grant.
        * Insert code here to dispose of the license. */
    }
}

控件許可的驗證代碼(RedistributableLicenseProvider)與控件本身的邏輯完全分離,分工協(xié)作保護(hù)組件的知識產(chǎn)權(quán)。

標(biāo)簽:南平 攀枝花 咸寧 拉薩 POS機(jī) 棗莊 廈門 益陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    来宾市| 土默特左旗| 门源| 五河县| 威海市| 北流市| 永仁县| 绵竹市| 鄱阳县| 武山县| 咸丰县| 通州区| 龙州县| 任丘市| 定远县| 开远市| 永年县| 将乐县| 禹州市| 酉阳| 乌拉特后旗| 泌阳县| 安塞县| 武功县| 无极县| 明水县| 淄博市| 青浦区| 霍林郭勒市| 大城县| 通山县| 酉阳| 岳阳市| 抚远县| 福泉市| 宁阳县| 双江| 介休市| 武川县| 浙江省| 讷河市|