How to get Public Key Token

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 <SafeControl /> 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

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, click 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 (In VS2008: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe)
    • Arguments: -Tp "$(TargetPath)"
    • Uncheck all options, except Use Output window\


Creating an external tool in Visual Studio 2005
(click to enlarge)

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
(click to enlarge)

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

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

Very cool! Thanks David!

(Source : http://www.andrewconnell.com/blog/archive/2006/09/15/4587.aspx)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: