A strong name is a cryptographic public/private key pair that is applied to an assembly in order to prove that it has not been tampered with after compilation. The process of strong naming an assembly has three components: creating the key, adding the key to the project, and adding the key to the assembly. We’ll look at each part in turn.
Creating the Key
The process of creating the key is relatively straight forward as Visual Studio ships with a utility that creates the key for you. Follow these steps to create the key:
- Start a Visual Studio Command prompt.
- Navigate to the project directory for the project.
- Enter the command ‘sn –k myproject.snk’ where myproject is the name of the project. The key that is created will have the name of myproject.snk.
- Close the command prompt.
You now have a strong name key which can be added to the project.
Adding the Key to the Project
Now that you have the key, you need to add it to the project. This is done to ensure that Visual Studio will manage the check in and check out process for you. Follow these steps to add the key to the project:
- Right click the project, select Add, Add an existing item
- Change the Files of type drop down to All Files (*.*)
- Select the myproject.snk file and Click the OK button. As before myproject should be replaced with your project name.
- Select File-Save All from the menu.
You’ve now added the key to the project. It will be checked in and out when you select those commands in Visual Studio.
Adding the Key to the Assembly
The final step in the creation of a strong name is to add the key to the assemblyinfo.cs file. This file was already added to your project for you. Here’s how to add the key to the assembly:
- Open the AssemblyInfo.cs file.
- Locate the line [assembly: AssemblyKeyFile(“”)]
- Swap the set of double quotes with @”..\..\myproject.snk” so that the line looks like [assembly: AssemblyKeyFile(@”..\..\myproject.snk”)]. As above, replace myproject with your project name. Note that the at sign says to C# that you don’t want to use escape characters. The pairs of periods are parent directory transitions. These are needed since the signing process takes place in either the bin\debug or bin\release directories – thus the key file, which is in the root of the project, is two levels above the assembly.
- Save the file.
You’re successfully added a strong name to an assembly.
No comment yet, add your voice below!