How to: Sign an Assembly with a Strong Name
There are a number of ways to sign an assembly with a strong name:
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.
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. 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.
In Solution Explorer, open the shortcut menu for the project, and then choose Properties.
Choose the Signing tab.
Select the Sign the assembly box.
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.
At the Visual Studio Command Prompt, type the following command:
al /out:<assemblyName> <moduleName> /keyfile:<keyfileName>
The name of the strongly signed assembly (a .dll or .exe file) that Assembly Linker will emit.
The name of a .NET Framework code module (a .netmodule file) that includes one or more types. You can create a .netmodule file by compiling your code with the
/target:moduleswitch in C# or Visual Basic.
The name of the container or file that contains the key pair. Assembly Linker interprets a relative path in relationship to the current directory.
The following example signs the assembly
MyAssembly.dll with a strong name by using the key file
al /out:MyAssembly.dll MyModule.netmodule /keyfile:sgKey.snk
For more information about this tool, see Assembly Linker.
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.
Compile the source code file normally.
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.
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.
You can also delay sign an assembly when compiling your source file. For more information, see Delay Signing an Assembly.
Compile your source code file or files with the
/delaysigncompiler option in C# and Visual Basic, or the
/DELAYSIGNlinker 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 Signing an Assembly.
The following example uses the C# compiler and signs the assembly
UtilityLibrary.dllwith a strong name by using the key file
csc /t:library UtilityLibrary.cs /keyfile:sgKey.snk