如何:使用强名称为程序集签名How to: Sign an Assembly with a Strong Name

可通过许多方法为程序集签署强名称: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 创建程序集并为程序集签署强名称To 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.

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

  • Visual Studio 命令提示处,键入以下命令:At the Visual Studio Command Prompt, type the following command:

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

    其中:where:

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

    moduleNamemoduleName
    包含一个或多个类型的 .NET Framework 代码模块的名称(.netmodule 文件)。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.

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

下面的示例使用密钥文件 MyAssembly.dll 为程序集 sgKey.snk签署强名称。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.

使用特性为程序集签署强名称To 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 Signing an Assembly.

使用编译器为程序集签署强名称To 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.

    有关延迟签名的信息,请参阅 Delay Signing an AssemblyFor information on delay signing, see Delay Signing an Assembly.

    下面的示例使用 C# 编译器并借助密钥文件 UtilityLibrary.dll 为程序集 sgKey.snk 签署强名称。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

创建和使用具有强名称的程序集Creating and Using Strong-Named Assemblies
如何:创建公钥/私钥对How to: Create a Public-Private Key Pair
Al.exe(程序集链接器)Al.exe (Assembly Linker)
延迟签发程序集Delay Signing an Assembly
管理程序集签名和清单签名Managing Assembly and Manifest Signing
“项目设计器”->“签名”页Signing Page, Project Designer