VS 2005 trick for obtaining the public key token and blob of a signed assembly

Friday, September 15, 2006 12:04 PM
Microsoft MVP Logo

David Bailey from RDA Corp clued me into a very slick way to obtain the public key token and blob for a signed assembly directly from within Visual Studio 2005. This is a common task for Web Part developers as we need the public key token when creating entries in the web.config among other places in SharePoint.

Previously I was opening the signed assembly and get the public key token using Lutz's Reflector. The public key token is displayed in the footer of Reflector, like so:

Obtaining public key token using Reflector
Obtaining public key token using Reflector (click image to see enlarged versions)

The way to get the public key token using the .NET 2.0 provided utilities is to open a Visual Studio 2005 command prompt and typing: sn.exe -T [full path to strong named assembly]

However, there's an even easier way using the external tools dialog in Visual Studio 2005 which David demonstrated in our SharePoint Developer class earlier today.

  1. In Visual Studio 2005, select Tools > External Tools…
  2. Click Add and enter the following into the different fields as displayed in the following screenshot:
    • Title: Get Public Key
    • Command: C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe
    • Arguments: -Tp “$(TargetPath)”
    • Uncheck all options, except Use Output window
Creating an external tool in Visual Studio 2005
Creating an external tool in Visual Studio 2005 (click image to see enlarged versions)

Now, you have a new entry listed in the Tools menu titled Get Public Key as shown in the following screenshot:

Get Public Key menu item in the Visual Studio Tools menu
Get Public Key menu item in the Visual Studio Tools menu (click image to see enlarged versions)

Assuming you have a project open that has been configured to be signed when built, and you've built it at least one time, selecting the new Get Public Key menu item from the Tools window to get the public key token and blob in the Output window, as shown here:

Results of Get Public Key menu item in the output window
Results of Get Public Key menu item in the output window (click image to see enlarged versions)

Very slick! You can even take this one step further by adding a button to a toolbar by customizing your toolbar and adding a button from the Tools category. you should pick External Command # where # is the index of the external command from the External Tools window (in my screenshots above, this is External Command 3). Then you can change the name of the button to be Get Public Key as shown below:

Custom button Get Public Key on the toolbar
Custom button Get Public Key on the toolbar (click image to see enlarged versions)

Very cool! Thanks David!

comments powered by Disqus