一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - 深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析

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

2019-11-13 11:23asp.net教程網(wǎng) ASP.NET教程

許可證編譯器 (Lc.exe) 的作用是讀取包含授權(quán)信息的文本文件,并產(chǎn)生一個可作為資源嵌入到公用語言運行庫可執(zhí)行文件中的 .licenses 文件

在使用第三方類庫時,經(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,開始源代碼分析之旅。

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

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

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

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


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

MSDN有完整的解釋,拷貝到下面方便您參考,以減少因查找MSDN引起思路中斷。
/complist:filename   指定包含授權(quán)組件列表的文件名,這些授權(quán)組件要包括到 .licenses 文件中。每個組件用它的全名引用,并且每行只有一個組件。命令行用戶可為項目中的每個窗體指定一個單獨的文件。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 委托的含義。如果運行庫類加載程序無法解析對程序集、類型或資源的引用,則將引發(fā)相應(yīng)的事件,從而使回調(diào)有機會通知運行庫引用的程序集、類型或資源位于哪個程序集中。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è)計時許可和運行時許可,從上面的例子中可以看到,設(shè)計時沒有限制,可以運行,但是到運行時,你必須有序列號,它才會生成許可對象,而不是返回null給.NET Framework類型。整個驗證過程由.NET完成。
你只需要像下面這樣,應(yīng)用這個許可保護(hù)機制:

復(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)。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产欧美综合一区二区 | 欧美大片一区二区 | 免费一级欧美片片线观看 | 国产黄色大片网站 | 日本福利视频网站 | 免费a视频在线观看 | 午夜精品亚洲 | 99久久综合九九亚洲 | 日韩毛片大全免费高清 | 师尊被各种play打屁股 | 国产v在线在线观看羞羞答答 | 7777奇米影视 | 丝瓜茄子绿巨人秋葵榴莲污 | 午夜伦伦电影理论片费看 | 9191视频| 99久久一香蕉国产线看观看 | 国产午夜永久福利视频在线观看 | 国产精品福利久久2020 | 亚洲欧美成人综合在线 | 亚洲 日韩 自拍 视频一区 | ass老妇黑森林pic | 色吧五月婷婷 | 亚洲精品国产一区二区第一页 | 日本视频免费在线 | 国内自拍网红在综合图区 | 韩国女主播在线大尺无遮挡 | 九九九久久久 | 国产精品免费综合一区视频 | 全日本爽视频在线 | 沉香如屑西瓜视频免费观看完整版 | 天天色视频| 女主被当众调教虐np | 欧美一区二区日韩一区二区 | 美女跪式抽搐gif动态图 | 风间由美一区二区av101 | 亚洲精品视频观看 | 欧美亚洲国产精品久久久 | 精品久久久久久影院免费 | 国产草草视频 | 国产v视频| 韩国三级大全 |