Andrew Connell [MVP MOSS]
1468 Posts |  40 Articles |  0 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
SharePoint Quick Links
Article Categories
Archives
Post Categories


Add to Technorati Favorites

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:

DavidBailey.SharePoint.TipsTricks.Reflector
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
    • Arguments: -Tp "$(TargetPath)"
    • Uncheck all options, except Use Output window\

DavidBailey.SharePoint.TipsTricks.ExternalTools
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:

DavidBailey.SharePoint.TipsTricks.ToolsMenu
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:

DavidBailey.SharePoint.TipsTricks.OutputWindow
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:

DavidBailey.SharePoint.TipsTricks.Toolbar
Custom button Get Public Key on the toolbar

Very cool! Thanks David!

Technorati: ,

posted on Friday, September 15, 2006 4:04 PM

Feedback

# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 9/18/2006 6:32 AM Jannik Anker
Gravatar Hey Andrew,

Just wanted to say that this trick is not just for VS2005 - I just tried it in VS2003, and it works great! Apart from that, cudos to David for an awesome trick :-)

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 9/19/2006 6:30 PM Steve T
Gravatar Thanks for that! I was in the class and forgot to get the details about this shortcut. Was just searching for it when I found your new blog entry.

# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 9/19/2006 9:34 PM AC [MVP MCMS]
Gravatar Jannik - Good to know... thanks for adding this comment!
Steve - No sweat... glad you found it ala Google!

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 9/21/2006 3:27 AM Aren
Gravatar Great!! Its working fo Vs2005. Thanks for the trick.

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 11/8/2006 12:08 PM Jon Coign
Gravatar Thanks for the tip, I have been using this one for a couple of months. You can also get the public key without the line breaks by using the same technique with secutil.

Command:
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\secutil.exe

Arguments:
-hex -s $(TargetPath)

# Using Visual Studio 2005, MakeCab.exe and MSBuild to Create Window SharePoint Services v3 Solution Files (*.WSP's) 12/20/2006 6:57 AM Andrew Connell [MVP MCMS]
Using Visual Studio 2005, MakeCab.exe and MSBuild to Create Window SharePoint Services v3 Solution Files (*.WSP's)

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 2/22/2007 8:30 AM Mc
Great tip!

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 3/19/2008 6:26 AM Sathish Kumar
Gravatar Hi,

Thanks for this trick.
It Working fine in dotnet 2005

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 5/11/2008 7:19 PM Pankaj
Gravatar AC your are great!
Awesome posting!!!

 For Visual Studio 2008 7/21/2008 2:35 PM Tim Howland
Gravatar For Visual Studio 2008, the path to sn.exe and others is:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 8/13/2008 9:38 AM Jeroen
Great! This worked for me! Thanks!

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 8/19/2008 6:14 AM prakash
Gravatar very nice. i took only 2 mins to get public key token. Very useful. thanks alot

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 9/29/2008 3:58 AM dinesh
Gravatar it's nice but i havn't get public key token
i get this

Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.

D:\Max\TestGraphics\TestGraphics\obj\Debug\TestGraphics.exe does not represent a strongly named assembly

could any one help me out

# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 9/29/2008 7:59 AM AC [MVP MOSS]
Gravatar Dinesh-
That means you haven't signed the project. Look in the VS help for assistance to do that.

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 10/16/2008 3:09 PM Mark
Gravatar Works in VS 2008 as well.

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/7/2009 6:25 AM Kieran
Hi Andrew

I've tried using your technique above to get the public key token but my "TargetPath" attribute seems to be empty.

My project is based on your CQWP article:

http://andrewconnell.com/blog/archive/2008/02/18/Subclassing-the-Content-Query-Web-Part-Adding-Dynamic-Filtering.aspx

When I try to run 'get public key' I get the following:

Failed to read -- The system cannot find the path specified.

Any ideas why this might be the case?

Many Thanks

# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/7/2009 9:45 AM AC [MVP MOSS]
Gravatar Kieran-
Sounds like you haven't built your project yet. TargetPath points to a DLL... and if you haven't build the project, then no DLL for you.

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/8/2009 6:54 AM Kieran
Andrew

Would this be because the project I am using:

http://andrewconnell.com/blog/archive/2008/02/18/Subclassing-the-Content-Query-Web-Part-Adding-Dynamic-Filtering.aspx

builds to a .wsp and .cab file rather than a .dll?

I'll unload the project and change the build properties to generate a .dll

All the best
Kieran


# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/8/2009 8:36 AM AC [MVP MOSS]
Gravatar Kieran-
That project doesn't just build WSP, it builds a DLL first thus no changes are required. Not sure why it isn't working on your machine... maybe a path or space in the path problem. I've never had this problem so not sure.

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/8/2009 10:02 AM Kieran
Hey Andrew

The project was actually building the .cab and .wsp files rather than the .dll. This seems to break the above approach to getting the key?

How do you handle this situation when building your projects, do you build the .dll and key, then change the build instructions to create the .wsp and .cab? This seems a bit of a pain to have to keep doing this :)

All the best

# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/8/2009 10:37 AM AC [MVP MOSS]
Gravatar Kieran-
Sorry, but you're wrong... it does build a DLL AND the CAB & WSP. The project first builds a DLL, then it runs a custom MSBuild Targets that then packages everything up into a WSP. It does this with each build of the project.

I show you how to do this here: http://www.andrewconnell.com/blog/articles/UsingVisualStudioAndMsBuildToCreateWssSolutions.aspx

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/9/2009 6:49 AM Kieran
Maybe I'm misunderstanding something but after re-reading your article, I dont see how it can build the DLL as well after making the changes to the .csproj file that your screenshot example shows?

All the best

# re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/9/2009 7:52 AM AC [MVP MOSS]
Gravatar Kieran-
Delete the BIN directory and build the project. You'll see the DLL get generated. I also show how it works in my book as well as in a screencast on MSDN: http://msdn.microsoft.com/en-us/library/cc441431.aspx

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/12/2009 4:40 AM Kieran
Hey Andrew

I was even telling myself it cant build the dll if I dont tell it to!

I needed the extra afterbuild option in there - sorted now.

Cheers
Kieran

 re: VS 2005 trick for obtaining the public key token and blob of a signed assembly 1/26/2009 9:44 AM Siegfried Glaser
Gravatar Thanks for sharing this neat little tidbit.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 4 and 1 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2009 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 5-day hands-on and online WCM classes for developers I offer through my company: Critical Path Training.

Get more information on the WCM courses!


Upcoming Classes
 Hands-on WCM:
 Online WCM:
 » July 20-24, 2009


» Register today!