How to solve the issues of PublicKeyToken and strong naming in .NET?

In .NET, the PublicKeyToken is a unique value used to identify an assembly, obtained by hashing the assembly’s public key. The PublicKeyToken can be used to ensure that the exact version of the assembly needed is referenced.

Strong naming refers to signing an assembly to ensure its identity and integrity. In .NET, strong naming can prevent assemblies from being tampered with or replaced. The process of strong naming typically involves generating a key pair, signing the assembly, and embedding the public key into the assembly.

One way to solve the strong naming issue is to use the tool “sn.exe” to generate a key pair, and add the public key to the assembly’s properties. The specific steps are as follows:

  1. Open the command prompt and navigate to the installation directory of the .NET Framework (usually located in the C:\Windows\Microsoft.NET\Framework\vX.X.X directory).
  2. Generate a key pair using the following command:
  3. Use the -k flag followed by the keypair.snk option.
  4. is the filename for the key pair, and it can be customized.
  5. In Visual Studio, open the properties page of the project and select the “Signing” tab.
  6. Check the “Sign the assembly” box and choose the key pair file that was just generated.
  7. When you recompile the project, the generated assembly will be strongly named.

Additionally, if referencing a strong-named assembly but unable to resolve the PublicKeyToken, it can be resolved using the following method:

  1. Use the tool “sn.exe” to retrieve the PublicKeyToken of this assembly. Execute the following command in the command prompt:
  2. Use the following command to run the assembly:
    sn -T
  3. The is the full path of the assembly.
  4. Update the PublicKeyToken to the correct value in the place where the assembly is referenced.

Here are some methods to solve the issues with PublicKeyToken and strong naming in .NET.

bannerAds