What is Strong Name?
Strong name is an indispensable security feature in .Net framework. Strong name is an assembly identity which gives uniqueness to an assembly in user computer. User can be assured about its unique identity. Strong name is a simple text file (encoded in some format) attached to an assembly. The only reason strong name is attached is to make assembly GACed which can be shared within multiple application.
Note: Assembly without strong name cannot be GACed.
Why Strong Name is required to sign an assembly?
Assembly is prepared by X organization and they delivered to their consumer. There is a probability that anyone can tempered or spoof assembly before reaching to actual consumer. Thus assembly makes an integrity check on assembly content and guarantees that no one tempered it.
Note: Strong name assembly can only reference strong name assembly.
How to generate Strong Name?
Let’s stop all theoretical concepts and let’s begin some practical scenarios about generating strong name and signing strong name to an assembly.
Step1: Open .Net framework command prompt
Figure 1 .Net framework Command Prompt
User needs to open .Net framework command prompt to generate strong name via SN.exe. SN.exe a .Net framework tool available in “../Microsoft.Net/SDK” folder. Try to run SN.exe on command prompt. You will get all SN.exe help. This ensures that sn.exe is avail in .Net framework SDK. There are many options avail in SN.exe which user can use based on their requirement. Following are the options and their usages.
Figure 2 SN.exe options
Step2: Generate Strong Name Key file.
Navigate to an application assembly path (i.e. path where *.dll file exists). Replace *.dll extension to *.snk (snk stands for strong name key). Press enter.
New SNK file will be generated with application name (having extension *.snk).
Signing an assembly with a strong name?
In step2, we generated strong name. However, we didn’t yet sign strong name to application assembly. Signing will be done by compiler. However before compilation, we need to make one application settings. Every .Net application provide Assembly.cs file, which meant for version control and signing assembly with strong name.
Step3: Edit in AssemblyInfo.cs:
Move to line AssemblyKeyFileAttribute(“”). This attribute takes *.snk filepath as a parameter.
If your *.snk file is available in “bin”. User needs syntax as AssemblyKeyFileAttribute (“../myassembly.snk”).
If your *.snk file is available in before “bin”. User needs syntax as AssemblyKeyFileAttribute(“../../myassembly.snk”).
Figure 3 Assembly.cs
Note: “../” i.e. double dot and forward slash.
Step4: Compile
Giving *.snk filepath doesn’t attach strong name. We only specified path for compiler to actually attach strong name to *.dll file. So, now user can compiler the application. If I ran successfully, that mean strong name is been attached to your application.
Step5: GACed (global assembly cache) assembly
We generate/attach strong name to an assembly when we need it to GACed it and share it within multiple application. Now user can GACed *.dll.
There are several ways to deploy an assembly into the GAC:
· Use a Windows Installer 2.0 designed to work with the GAC. This is the preferred option for installing assemblies into the GAC.
· Use a developer tool called the Global Assembly Cache tool (Gacutil.exe), provided by the .NET Framework SDK.
· Use Windows Explorer to drag assemblies into the cache.
Note: Common way is to drag-n-drop your assembly in GAC. GAC is available in <DRIVE>:\WINDOWS\assembly
There is one more way to sign an assembly with .Net Framework SDK known as Assembly Linker.
User can get a very good article on MSDN over here