如何:使用强名称为程序集签名How to: Sign an assembly with a strong name

备注

虽然 .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.

可通过许多方法为程序集签署强名称:There are a number of ways to sign an assembly with a strong name:

  • 在 Visual Studio 中,通过使用项目的 “属性” 对话框中的 “签名” 选项卡。By using the Signing tab in a project's Properties dialog box in Visual Studio. 这是为程序集签署强名称的最简单且最方便的方法。This is the easiest and most convenient way to sign an assembly with a strong name.

  • 通过使用程序集链接器 (Al.exe) 来链接 .NET Framework 代码模块(.netmodule 文件)与密钥文件。By using the Assembly Linker (Al.exe) to link a .NET Framework code module (a .netmodule file) with a key file.

  • 通过使用程序集特性将强名称信息插入代码中。By using assembly attributes to insert the strong name information into your code. 你可以使用 AssemblyKeyFileAttributeAssemblyKeyNameAttribute 特性,具体取决于要使用的密钥文件所在的位置。You can use either the AssemblyKeyFileAttribute or the AssemblyKeyNameAttribute attribute, depending on where the key file to be used is located.

  • 通过使用编译器选项。By using compiler options.

要使用强名称为程序集签名,必须具有加密密钥对。You must have a cryptographic key pair to sign an assembly with a strong name. 有关创建密钥对的详细信息,请参阅如何:创建公钥/私钥对For more information about creating a key pair, see How to: Create a public-private key pair.

使用 Visual Studio 创建程序集并使用强名称为程序集签名Create and sign an assembly with a strong name by using Visual Studio

  1. 在“解决方案资源管理器” 中,打开项目的快捷菜单,然后选择“属性” 。In Solution Explorer , open the shortcut menu for the project, and then choose Properties.

  2. 选择 “签名” 选项卡。Choose the Signing tab.

  3. 选择“为程序集签名”框。Select the Sign the assembly box.

  4. 在“选择强名称密钥文件”框中,选择“浏览”,然后导航到密钥文件 。In the Choose a strong name key file box, choose Browse , and then navigate to the key file. 若要创建新的密钥文件,请选择“新建”,并在“创建强名称密钥”对话框中输入其名称 。To create a new key file, choose New and enter its name in the Create Strong Name Key dialog box.

备注

为了延迟为程序集签名,请选择公钥文件。In order to delay sign an assembly, choose a public key file.

使用程序集链接器创建程序集并使用强名称为程序集签名Create and sign an assembly with a strong name by using the Assembly Linker

Visual Studio 开发人员命令提示处,输入以下命令:At the Developer Command Prompt for Visual Studio, enter the following command:

al /out:<assemblyName> <moduleName> /keyfile:<keyfileName> al /out:<assemblyName> <moduleName> /keyfile:<keyfileName>

其中:Where:

  • assemblyName 是程序集链接器将发出的强签名程序集(.dll 或 .exe 文件)的名称 。assemblyName is the name of the strongly signed assembly (a .dll or .exe file) that Assembly Linker will emit.

  • moduleName 是包含一个或多个类型的 .NET Framework 代码模块(.netmodule 文件)的名称 。moduleName is the name of a .NET Framework code module (a .netmodule file) that includes one or more types. 可通过在 C# 或 Visual Basic 中使用 /target:module 开关对代码进行编译来创建.netmodule 文件。You can create a .netmodule file by compiling your code with the /target:module switch in C# or Visual Basic.

  • keyfileName 是包含密钥对的容器或文件的名称。keyfileName is the name of the container or file that contains the key pair. 程序集链接器解释与当前目录相关的相对路径。Assembly Linker interprets a relative path in relation to the current directory.

下面的示例使用密钥文件 sgKey.snk 为程序集 MyAssembly.dll 签署强名称 。The following example signs the assembly MyAssembly.dll with a strong name by using the key file sgKey.snk.

al /out:MyAssembly.dll MyModule.netmodule /keyfile:sgKey.snk  

有关此工具的详细信息,请参阅 程序集链接器For more information about this tool, see Assembly Linker.

使用属性为程序集签署强名称Sign an assembly with a strong name by using attributes

  1. System.Reflection.AssemblyKeyFileAttributeAssemblyKeyNameAttribute 特性添加到源代码文件中,并指定包含为程序集签署强名称时要使用的密钥对的文件或容器的名称。Add the System.Reflection.AssemblyKeyFileAttribute or AssemblyKeyNameAttribute attribute to your source code file, and specify the name of the file or container that contains the key pair to use when signing the assembly with a strong name.

  2. 通常会编译源代码文件。Compile the source code file normally.

    备注

    当 C# 和 Visual Basic 编译器在源代码中遇到 AssemblyKeyFileAttributeAssemblyKeyNameAttribute 特性时,会发出编译器警告(分别为 CS1699 和 BC41008)。The C# and Visual Basic compilers issue compiler warnings (CS1699 and BC41008, respectively) when they encounter the AssemblyKeyFileAttribute or AssemblyKeyNameAttribute attribute in source code. 你可以忽略这些警告。You can ignore the warnings.

下面的代码示例将 AssemblyKeyFileAttribute 属性用于名为 keyfile.snk 的密钥文件(位于编译程序集的目录中)。The following example uses the AssemblyKeyFileAttribute attribute with a key file called keyfile.snk , which is located in the directory where the assembly is compiled.

[assembly:AssemblyKeyFileAttribute("keyfile.snk")];
[assembly:AssemblyKeyFileAttribute("keyfile.snk")]
<Assembly:AssemblyKeyFileAttribute("keyfile.snk")>

在编译源文件时,也可以延迟为程序集签名。You can also delay sign an assembly when compiling your source file. 有关详细信息,请参阅延迟为程序集签名For more information, see Delay-sign an assembly.

使用编译器为程序集签署强名称Sign an assembly with a strong name by using the compiler

使用 C# 和 Visual Basic 中的 /keyfile/delaysign 编译器选项,或使用 C++ 中的 /KEYFILE/DELAYSIGN 链接器选项编译源代码文件。Compile your source code file or files with the /keyfile or /delaysign compiler option in C# and Visual Basic, or the /KEYFILE or /DELAYSIGN linker option in C++. 在选项名称后,添加冒号和密钥文件的名称。After the option name, add a colon and the name of the key file. 使用命令行编译器时,你可以将密钥文件复制到包含源代码文件的目录中。When using command-line compilers, you can copy the key file to the directory that contains your source code files.

有关延迟签名的信息,请参阅延迟为程序集签名For information on delay signing, see Delay-sign an assembly.

下面的示例使用 C# 编译器并借助密钥文件 sgKey.snk 为程序集 UtilityLibrary.dll 签署强名称 。The following example uses the C# compiler and signs the assembly UtilityLibrary.dll with a strong name by using the key file sgKey.snk.

csc /t:library UtilityLibrary.cs /keyfile:sgKey.snk  

请参阅See also