Signing assemblies

Creating a unique identity for an assembly is referred to as signing or strong-naming an assembly. Providing a unique identity for an assembly avoids assembly conflicts. Each assembly maintains hashes of the modules, resources, and file information in its manifest. When you sign an assembly, the following is captured:

  • The name of the assembly
  • The version number of the assembly
  • If available, the culture (also called locale for code development) of the assembly
  • The public key that is used to sign the assembly to the assembly manifest

Signing an assembly provides the following benefits:

  1. It allows us to give a friend access to other signed assemblies.
  2. It allows us to run different versions of the same assembly side by side.
  3. It allows us to deploy our assembly into the GAC. This allows other applications to use our assembly as well.

You can sign an assembly in two ways: the first is by using Visual Studio, and the second is by using the command-line tool. Visual Studio makes it simple to sign the assembly.

Here, we will demonstrate how to sign an assembly using Visual Studio:

  1. Navigate to Project Properties.
  2. Navigate to the Signing* tab on the left side:

  1. Select the Sign the assembly checkbox.
  2. Choose <New...> in the drop-down menu, and then choose a strong key filename:

  1. Give a key filename in the pop-up window.
  2. Visual Studio allows you to choose an algorithm and provide a password for the key file.
  3. The password is optional; an algorithm can be changed as per your requirements from the available list.

Alternatively, we can sign an assembly using Command Prompt and Visual Studio tools that come with the following installation steps:

  1. Click the Windows button on your system.
  2. Navigate through the installed programs.
  3. Find the Visual Studio 20xx installation folder.
  4. Select Developer Command Prompt for Visual Studio 20xx.

 

  1. Use sn.exe to generate a strong name pair into a .snk file:
sn -k keyPair.snk
  1. Once the key file is created, you can now use Visual Studio to sign the assembly or use the al.exe tool.
  2. Use al.exe to link the assembly and key pair generated in the preceding steps:
al /out:chapter12.dll MyModule.netmodule /keyfile:keyPair.snk

More information about these commands is available on MSDN (https://docs.microsoft.com/en-us/dotnet/framework/tools/sn-exe-strong-name-tool).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.154.139