All paths in the command line instructions below assume all files are in the c:\ root. Enter full paths where necessary. The commands themselves (sn, ildasm, ilasm) can be run from anywhere if you're in a VS.NET command prompt which is different from a regular command prompt. To launch a VS.NET command prompt, you'll find a shortcut in the tools subgroup in the VS.NET installation program group in your start menu.
When you're building assemblies, it's not hard to apply a strong name to them.
- Create a strong name assembly key fileFrom a VS.NET command prompt, use the SN.EXE utility to create a file containing the public key/private key pair:c:\>sn -k c:\keypair001.snk
- Add reference to the assembly key file to your assembly projectOpen the AssemblyInfo.cs file in your project and add the following attribute:[assembly: AssemblyKeyFile(@"c:\keypair001.snk")]
- Compile your assemblySame way you would any other day of the week.
- Obtain the public key to your key fileYou may need this in the future. After you build your assembly, from a VS.NET command prompt, use the SN.EXE utility again to get the public key of your key pair:c:\>sn -T yourAssembly.dll
- (Added 12/16) Obtain the public key blob to your key fileYou'll need this in CAS policy files... it's the hex public key blob... use the SECUTIL.EXE utility:c:\>secutil -hex -s yourAssembly.dll
However, what if you need to apply a strong name to an assembly that was provided, already compiled, to you and you don't have the source? You first will need to produce the Microsoft intermediate language (MSIL) for the assembly using the ILDASM.EXE utility, then use an assembly key file to sign that MSIL into a new DLL using the ILASM.EXE utility.
- Obtain the MSIL for the provided assemblyFrom a VS.NET command prompt, enter the following:c:\>ildasm providedAssembly.dll /out:providedAssembly.il
- Rename/move the original assemblyI just tack on ".orig" to the filename.
- Create a new assembly from the MSIL output and your assembly keyfileAssuming you already have an assembly key pair file (if not, see #1 in previous steps), do the following from a VS.NET command prompt:c:\>ilasm providedAssembly.il /dll /key=keypair001.snk
You run into a problem when you get a failure when generating the DLL from the MSIL and. For example, I keep having a problem when using this method to make the Microsoft Data Application Block have a strong name for use in a SharePoint web part. I ~really~ don't want to build it with a strong name myself, I want to rebuild the one that's included from the downloadable MSI. Ideas anyone?
Update 12/16: Added links to the four .NET utilities (sn, secutil, ildasm, ilasm) referenced in this article.