Wednesday, March 2, 2011

Strong Name Key - Access Denied

After rebuilding my machine a few weeks ago, once again I find myself running into old problems and trying to remember how to repair them. This time, generating keys to strongly name my asesmblies through the Signing tab in Visual Studio 2008. I thought I would share it this time in case others have seen this problem...and struggled with it...

The Scenario:

For a Windows client application, open the Properties for the project, navigate to the Signing tab, check Sign the assembly and create a new key. In the Create Strong Name Key dialog, provide a new key name, provide a password, and click "OK".

The Problem:

An error message is presented: Create strong name key the operation could not be completed. Access is denied.

The problem repeats itself when I tried to use the strong name utility (sn.exe), with this error: Failed to generate strong name key pair. Access is denied.

The Solution:

I had to give my user account access to the key container in C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys. This is because you need to access the key store to create a strong name key. Modifying permissions did not work in my case, so I needed to reset the location of my key store. This can be done by opening the Visual Studio Command Prompt as an Administrator (right click on the start menu - run as Administrator). Reset the key store by typing the command:
sn.exe -c

How does it work?

Each container created using sn.exe -i is located in the MachineKeys directory (unless you specify elsewhere). The default key container that is used by sn.exe is also in that location.

In the event you reset your key container to a new one, and forget where it is...you can reset the key container for the strong name utility using sn.exe -c. So, if the account access fix doesn't work, you may be using an alternate key store so a reset may be in order.

I googled this topic a bit before writing this blog, and was surprised that very little has been written on this subject for a solution...furthermore...many people actually rebuilt their machines to solve the problem!!!! I hope this blog entry prevents a few more of those!!!

No comments:

Post a Comment