Saturday, March 29, 2014

SNK, projects, users and key containers

It's always been irritating that when you add a strong name (snk) file to a Visual Studio project that it will make a copy of the file in the project folder. You can finish up with dozens of copies of the snk file scattered around your projects.

It would be nice to put a single snk file in a well-known location and have all projects reference that file, but the Visual Studio project properties do not seem to support this (if I'm wrong, please explain the trick to me!). To share a snk file it seems necessary to edit the csproj files manually and add something like this:

<PropertyGroup>
  <SignAssembly>true</SignAssembly>
  <AssemblyOriginatorKeyFile>..\Common Files\MyCompany.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

Another way of sharing a strong name is to put it into a named key container like this:

sn.exe -i MyCompany.snk MyCompany

This puts the snk file data into a protected part of the registry and avoids the need to have the snk file lying around at all. The Visual Studio project properties do not seem to support this either, so you'll have to edit the csproj file and add something like this:

<PropertyGroup>
  <KeyContainerName>MyCompany</KeyContainerName>
</PropertyGroup>

This was working well, but a day later I ran Visual Studio as Administrator and compiles failed telling me that the key container was not found. I eventually found I had to run this command to make the key container name available to all users:

sn -m n

The documentation on this switch is a bit misleading. This means that keys are not user specific (as I thought), but they are available for all users.

No comments:

Post a Comment