21 April 2015

Getting PublicKeyToken of an Assembly

Public key token of an assembly is small number representing the public key of the assembly. As the public keys are very long, the tokens allow us to refer the public key without specifying the whole key.
Public key token is the last 8 bytes of the SHA-1 hash of the public key under which the assembly is signed. The assembly must be signed to have a public key
To get PublicKeyToken of an assembly open Visual Studio command prompt and enter the following command –
sn –T NameOfDll.dll
where NameOfDll is the fully qualified name of the DLL for which you want to see the key token.

To simplify the process, we can also add this feature in Visual Studio Tool menu, so that we can get the key from within Visual Studio with just a clock of a button.
1.       From the Tools menu in Visual Studio, select ExternalTools.

2.       Add a new command say GetAssemblyToken and enter the parameters as follows.

The command is the full path of the Strong Name application i.e. sn.exe. If you do not know the full path of the application, just open the Visual Studio command prompt enter command, where sn.exe.
Make sure to check the Use Output Window so that the key is displayed in the Visual Studio output window.

Now if you need to get the PublicKeyToken of a signed assembly, just select the assembly in solution explorer and click the external tool that you have just created, and you should see the key in VS output window.
If we require getting the lengthy public key also, just do a little change while creating external tool command. In place of –T $(TargetPath), just type –TP $(TargetPath) as command argument.

No comments:

Post a Comment