• .NET框架程序设计Globally Deployment Assembly全局部署程序集


    Globally Deployment Assembly全局部署程序集

    (一)StrongName Assembly

    Strongly Named Assembly是CLR唯一标识程序集的机制,包含4个特性:
    ·文件名(没有扩展名)
    ·版本号
    ·语言文化
    ·公有密钥标记

    例如:MyTypes,Version=1.0.123.0,Culture=neutral,PublicKeyToken=1234567890123456

    利用 SN.exe 产生一个公钥/私钥对

    SN -k mycompany.keys

    查看公钥

    sn -p mycompany.keys mycompany.publickey
    sn -tp mycompany.publickey

    Microsoft (R) .NET Framework 强名称实用工具版本 1.1.4322.573
    Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

    公钥为
    0024000004800000940000000602000000240000525341310004000001000100830304ce5787ef
    d2355d71fa73739ea27581329409f9b2d4aaa38409a0aba5b77bfa34ffe425180b86ba432a4f21
    52f9f6c2aceb005d57208f7b3d48bd569110e91d7cdbdf534f20ed822650311c69ddda11fb721e
    128de231a6a0661aba1eb3e306acf2dc0777fc266612c4d7491118657cc741d99090a95bdfd969
    0b885ac8

    公钥标记为 91e5e7845907a9e6



    创建StrongName Assembly(using System.Relfection):

    [assembly:AssemblyKeyFile("mycompany.keys")]


    StrongName Assembly的元数据信息:

    Assembly
    -------------------------------------------------------
     Token: 0x20000001
     Name : Hello
     Public Key    : 00 24 00 00 04 80 00 00  94 00 00 00 06 02 00 00  00 24 00 00 52 53 41 31
                          : 00 04 00 00 01 00 01 00  83 03 04 ce 57 87 ef d2  35 5d 71 fa 73 73 9e a2
                          : 75 81 32 94 09 f9 b2 d4  aa a3 84 09 a0 ab a5 b7  7b fa 34 ff e4 25 18 0b
                          : 86 ba 43 2a 4f 21 52 f9  f6 c2 ac eb 00 5d 57 20  8f 7b 3d 48 bd 56 91 10
                          : e9 1d 7c db df 53 4f 20  ed 82 26 50 31 1c 69 dd  da 11 fb 72 1e 12 8d e2
                          : 31 a6 a0 66 1a ba 1e b3  e3 06 ac f2 dc 07 77 fc  26 66 12 c4 d7 49 11 18
                          : 65 7c c7 41 d9 90 90 a9  5b df d9 69 0b 88 5a c8
     Hash Algorithm : 0x00008004
     Major Version: 0x00000000
     Minor Version: 0x00000000
     Build Number: 0x00000000
     Revision Number: 0x00000000
     Locale: <null>
     Flags : [SideBySideCompatible] [PublicKey]  (00000001)

    AssemblyRef #1
    -------------------------------------------------------
     Token: 0x23000001
     Public Key or Token: b7 7a 5c 56 19 34 e0 89
     Name: mscorlib
     Major Version: 0x00000001
     Minor Version: 0x00000000
     Build Number: 0x00001388
     Revision Number: 0x00000000
     Locale: <null>
     HashValue Blob:
     Flags: [none] (00000000)



    (二)GAC(Global Assembly Cache)


    c:\windows\Assembly\GAC



    使用工具GACUtil.exe来实现安装卸载:

    安装一个StrongNameAssembly:
    gacutil.exe /i myassembly.dll

    卸载一个StrongNameAssembly:
    gacutil.exe /u myassembly.dll

    在DOS窗口下查看细节:





    (三)引用StrongNameAssembly

    我们发现安装DotNetFramwork的机器会有两份程序集文件,一份是在C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\zh-CHS中,另一份则是在GAC中。

    目的是方便应用程序的引用和加载.

    引用是指我们在编译程序的时候,如果需要引用别的程序集,则编译器会按照这样的目录查找:
    (1)使用编译器的/reference指定的文件的完全路径,如果只是程序集名称,则继续寻找
    (2)当前工作目录
    (3)CLR所在目录(C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\zh-CHS)
    (4)使用编译器的/lib指定的目录
    (5)LIB环境变量指定的目录


    加载是指应用程序运行的时候加载需要的程序集的路径(加载顺序在前面已经提到)


    利用响应文件(.rsp)来设置编译器的命令开关:

    例如,创建一个myproject.rsp文件:
    /out:myproject.exe
    /target:winexe
    编译的时候使用这个文件
    csc @myproject.rsp file1.cs file2.cs
    那么就等于我们不用这个响应文件的命令行:
    csc /out:myproject.exe /target:winexe file1.cs file2.cs

    在C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322里面有个csc.rsp文件

    # This file contains command-line options that the C#
    # command line compiler (CSC) will process as part
    # of every compilation, unless the "/noconfig" option
    # is specified.

    # Reference the common Framework libraries
    /r:Accessibility.dll
    /r:Microsoft.Vsa.dll
    /r:System.Configuration.Install.dll
    /r:System.Data.dll
    /r:System.Design.dll
    /r:System.DirectoryServices.dll
    /r:System.dll
    /r:System.Drawing.Design.dll
    /r:System.Drawing.dll
    /r:System.EnterpriseServices.dll
    /r:System.Management.dll
    /r:System.Messaging.dll
    /r:System.Runtime.Remoting.dll
    /r:System.Runtime.Serialization.Formatters.Soap.dll
    /r:System.Security.dll
    /r:System.ServiceProcess.dll
    /r:System.Web.dll
    /r:System.Web.Mobile.dll
    /r:System.Web.RegularExpressions.dll
    /r:System.Web.Services.dll
    /r:System.Windows.Forms.Dll
    /r:System.XML.dll



    (四)StrongNameAssembly的防篡改特性

    在安装StrongNameAssembly到GAC的时候,系统将会自动验证程序集是否被篡改,在从GAC加载的时候又一次以不同的方式验证,如果从别的非GAC目录加载,也会对程序集进行严密验证。

    (五)延迟签名

    为了防止私有密钥的泄漏,我们可以使用延迟签名技术(Delayed Singning).

    (1)取得公有密钥的文件,并将下面两个特性加入到源代码:
    [assembly:AssemlyKeyFile("MyCompanyPublicKey.keys")]
    [assembly:AssemlyDelaySign(true)]
    (2)生成Assembly的时候,执行下面的命令可以将Assembly安装到GAC中(一次操作即可)
    SN.exe -Vr MyAssembly.dll
    (3)打包部署应用程序时,取得公有/私有密钥对,执行命令:
    SN.exe -R MyAssembly.dll MyCompanyKey.Keys
    (4)执行下面的命令,恢复验证过程进行测试:
    SN.exe -Vu MyAssembly.dll

  • 相关阅读:
    Delegate、Predicate、Action和Func
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/caca/p/56346.html
Copyright © 2020-2023  润新知