创建和使用具有强名称的程序集Create and use strong-named assemblies

强名称是由程序集的标识加上公钥和数字签名组成的。其中,程序集的标识包括简单文本名称、版本号和区域性信息(如果提供的话)。A strong name consists of the assembly's identity—its simple text name, version number, and culture information (if provided)—plus a public key and a digital signature. 它使用相应私钥从程序集文件生成。It is generated from an assembly file using the corresponding private key. (程序集文件包含程序集清单,该清单包含组成程序集的所有文件的名称和哈希。)(The assembly file contains the assembly manifest, which contains the names and hashes of all the files that make up the assembly.)

警告

不要依赖于通过强名称实现安全性。Do not rely on strong names for security. 它们仅提供唯一的标识。They provide a unique identity only.

具有强名称的程序集只能使用其他具有强名称的程序集的类型。A strong-named assembly can only use types from other strong-named assemblies. 否则,将会危害强名称程序集的完整性。Otherwise, the integrity of the strong-named assembly would be compromised.

备注

虽然 .NET Core 支持强名称程序集,而且 .NET Core 库中的所有程序集均已签名,但大多数第三方程序集不需要强名称。Although .NET Core supports strong-named assemblies, and all assemblies in the .NET Core library are signed, the majority of third-party assemblies do not need strong names. 有关详细信息,请参阅 GitHub 上的强名称签名For more information, see Strong Name Signing on GitHub.

强名称方案Strong name scenario

下面的方案概述了对具有强名称的程序集进行签名以及稍后使用该名称对其进行引用的过程。The following scenario outlines the process of signing an assembly with a strong name and later referencing it by that name.

  1. 使用以下方法之一创建具有强名称的程序集 A:Assembly A is created with a strong name using one of the following methods:

    • 使用支持创建强名称的开发环境,如 Visual Studio。Using a development environment that supports creating strong names, such as Visual Studio.

    • 使用强名称工具 (Sn.exe) 创建加密密钥对,并使用命令行编译器或程序集链接器 (Al.exe) 将密钥对分配到程序集。Creating a cryptographic key pair using the Strong Name tool (Sn.exe) and assigning that key pair to the assembly using either a command-line compiler or the Assembly Linker (Al.exe). Windows SDK 同时提供 Sn.exe 和 Al.exe。The Windows SDK provides both Sn.exe and Al.exe.

  2. 开发环境或工具对包含具有开发人员私钥的程序集清单的文件的哈希进行签名。The development environment or tool signs the hash of the file containing the assembly's manifest with the developer's private key. 此数字签名存储在包含程序集 A 的清单的可移植可执行 (PE) 文件中。This digital signature is stored in the portable executable (PE) file that contains Assembly A's manifest.

  3. 程序集 B 是程序集 A 的一个使用者。程序集 B 清单的引用部分包含表示程序集 A 公钥的标记。Assembly B is a consumer of Assembly A. The reference section of Assembly B's manifest includes a token that represents Assembly A's public key. 标记是完整公钥的一部分,并且使用它而不是密钥本身以节省空间。A token is a portion of the full public key and is used rather than the key itself to save space.

  4. 当程序集放置在全局程序集缓存中时,公共语言运行时验证强名称签名。The common language runtime verifies the strong name signature when the assembly is placed in the global assembly cache. 在运行时按强名称绑定时,公共语言运行时会将存储在程序集 B 的清单中的密钥与为程序集 A 生成强名称的密钥进行比较。如果 .NET 安全检查通过且绑定成功,程序集 B 就可保证程序集 A 的位未被篡改,且这些位确实来自程序集 A 的开发人员。When binding by strong name at run time, the common language runtime compares the key stored in Assembly B's manifest with the key used to generate the strong name for Assembly A. If the .NET security checks pass and the bind succeeds, Assembly B has a guarantee that Assembly A's bits have not been tampered with and that these bits actually come from the developers of Assembly A.

备注

此方案不解决信任问题。This scenario doesn't address trust issues. 除强名称外,程序集可携带完整的 Microsoft Authenticode 签名。Assemblies can carry full Microsoft Authenticode signatures in addition to a strong name. Authenticode 签名包括建立信任的证书。Authenticode signatures include a certificate that establishes trust. 请务必注意强名称不要求代码以这种方式进行签名。It's important to note that strong names don't require code to be signed in this way. 强名称仅提供唯一的标识。Strong names only provide a unique identity.

跳过受信任程序集的签名验证Bypass signature verification of trusted assemblies

从 .NET Framework 3.5 Service Pack 1 开始,当程序集加载到完全信任的应用程序域(如 MyComputer 区域的默认应用程序域)时,不会验证强名称签名。Starting with the .NET Framework 3.5 Service Pack 1, strong-name signatures are not validated when an assembly is loaded into a full-trust application domain, such as the default application domain for the MyComputer zone. 这被称之为强名称跳过功能。This is referred to as the strong-name bypass feature. 在完全信任的环境中,对于已签名的完全信任的程序集,对 StrongNameIdentityPermission 的需求总是成功,而不考虑其签名。In a full-trust environment, demands for StrongNameIdentityPermission always succeed for signed, full-trust assemblies, regardless of their signature. 这种情况下,强名称跳过功能可避免完全信任程序集不必要的强名称签名验证开销,允许更快地加载程序集。The strong-name bypass feature avoids the unnecessary overhead of strong-name signature verification of full-trust assemblies in this situation, allowing the assemblies to load faster.

跳过功能适用于使用强名称进行签名及具有以下特征的任何程序集:The bypass feature applies to any assembly that is signed with a strong name and that has the following characteristics:

  • 完全受信任,无需 StrongName 证据(如具有 MyComputer 区域证据)。Fully trusted without StrongName evidence (for example, has MyComputer zone evidence).

  • 加载到完全受信任的 AppDomainLoaded into a fully trusted AppDomain.

  • 加载自该 AppDomainApplicationBase 属性下的某个位置。Loaded from a location under the ApplicationBase property of that AppDomain.

  • 签名没有延迟。Not delay-signed.

可为单个应用程序或计算机禁用此功能。This feature can be disabled for individual applications or for a computer. 请参阅如何:禁用强名称跳过功能See How to: Disable the strong-name bypass feature.

TitleTitle 描述Description
如何:创建公钥/私钥对How to: Create a public-private key pair 描述如何创建加密密钥对以对程序集进行签名。Describes how to create a cryptographic key pair for signing an assembly.
如何:使用强名称为程序集签名How to: Sign an assembly with a strong name 介绍如何创建具有强名称的程序集。Describes how to create a strong-named assembly.
改进的强命名Enhanced strong naming 描述 .NET Framework 4.5 中强名称的改进。Describes enhancements to strong-names in the .NET Framework 4.5.
如何:引用具有强名称的程序集How to: Reference a strong-named assembly 介绍如何在编译时或运行时引用具有强名称的程序集中的类型或资源。Describes how to reference types or resources in a strong-named assembly at compile time or run time.
如何:禁用强名称跳过功能How to: Disable the strong-name bypass feature 描述如何禁用跳过强名称签名验证的功能。Describes how to disable the feature that bypasses the validation of strong-name signatures. 可为所有或特定应用程序禁用此功能。This feature can be disabled for all or for specific applications.
创建程序集Create assemblies 提供单个文件和多文件程序集的概述。Provides an overview of single-file and multifile assemblies.
如何在 Visual Studio 中延迟对程序集的签名How to delay sign an assembly in Visual Studio 说明如何在创建程序集后对具有强名称的程序集进行签名。Explains how to sign an assembly with a strong name after the assembly has been created.
Sn.exe(强名称工具)Sn.exe (Strong Name tool) 介绍 .NET Framework 中包含的可帮助创建具有强名称的程序集的工具。Describes the tool included in the .NET Framework that helps create assemblies with strong names. 此工具提供有关密钥管理、签名生成和签名验证的选项。This tool provides options for key management, signature generation, and signature verification.
Al.exe(程序集链接器)Al.exe (Assembly linker) 介绍 .NET Framework 中包含的一种工具,该工具可生成具有模块或资源文件的程序集清单的文件。Describes the tool included in the .NET Framework that generates a file that has an assembly manifest from modules or resource files.