好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

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

熱門標簽:辦理400電話證件 十堰正規電銷機器人系統 世紀佳緣地圖標注怎么去掉 手機地圖標注如何刪除 巫師3為什么地圖標注的財寶沒有 寧波自動外呼系統代理 外呼系統代理品牌 怎么給超市做地圖標注入駐店 外呼系統費用一年

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

復制代碼 代碼如下:

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

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

控件名稱, 程序集全名稱

首先根據需要,寫一個需要被授權的控件列表,格式如上所示。例如,HostApp.exe 的應用程序要引用Samples.DLL 中的授權控件 MyCompany.Samples.LicControl1,則可以創建包含以下內容的 HostAppLic.txt。 MyCompany.Samples.LicControl1, Samples.DLL。

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

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

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

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

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



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

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



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

  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 委托的含義。如果運行庫類加載程序無法解析對程序集、類型或資源的引用,則將引發相應的事件,從而使回調有機會通知運行庫引用的程序集、類型或資源位于哪個程序集中。ResolveEventHandler 負責返回解析類型、程序集或資源的程序集。
復制代碼 代碼如下:

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

對第一部參數分析出來的組件列表,依次循環,為它們產生授權許可
復制代碼 代碼如下:

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

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

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推薦的保護組件的方式,與我們平時所討論的輸入序列號,RSA簽名不同。
來看一下,商業的組件是如何應用這種技術保護組件的。
復制代碼 代碼如下:

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
}

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

[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)與控件本身的邏輯完全分離,分工協作保護組件的知識產權。

標簽:天門 山西 牡丹江 嘉興 平涼 通遼 泰州 景德鎮

巨人網絡通訊聲明:本文標題《深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析》,本文關鍵詞  深入,解析,.NET,許可證,編譯器,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析》相關的同類信息!
  • 本頁收集關于深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析的相關信息資訊供網民參考!
  • 推薦文章