May 9, 2021

How to find the PublicKeyToken for .Net assembly

The public key token is a unique 16-character key that is given to the assembly when it is built and signed in Microsoft Visual Studio.

Often you need to specifiy Public-Key-Token, like when you have to define a provider in connectionString in web.config, or referencing external dlls in web.config, in these cases you need to know the Public-Key-Token for that particular assembly or dll.

There are two ways you can find the Public-Key-Token for an assembly:

Using Powershell

You can execute this statement:

([system.reflection.assembly]::loadfile("C:\FullPath..\Newtonsoft.Json.dll")).FullName

The output will provide the Version, Culture and PublicKeyToken as shown below:

Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

Using the Strong Name tool (SN.exe)

Open the Visual Studio Command Prompt and then point to the dll’s folder you want to get the public key, use the following command,

sn –T Newtonsoft.Json.dll

Or specify the full path of the dll.

sn –T "C:\FullPath..\Newtonsoft.Json.dll"

This will give you the public key token.

Remember this only works if the assembly is strongly signed.

No comments:

Post a Comment