Andrew Connell

HOWTO: Strongly name prebuilt assemblies

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.

  1. 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
  2. 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")]
  3. Compile your assemblySame way you would any other day of the week.
  4. 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
  5. 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.

  1. Obtain the MSIL for the provided assemblyFrom a VS.NET command prompt, enter the following: c:\>ildasm providedAssembly.dll /out:providedAssembly.il
  2. Rename/move the original assemblyI just tack on “.orig” to the filename.
  3. 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?

Andrew Connell
Founder & Chief Course Artisan, Voitanos LLC. | Microsoft MVP
Written by Andrew Connell

Andrew Connell is a web developer with a focus on Microsoft Azure & Microsoft 365. He’s received Microsoft’s MVP award every year since 2005 and has helped thousands of developers through the various courses he’s authored & taught. Andrew’s the founder of Voitanos and is dedicated to delivering industry-leading on-demand video training to professional developers. He lives with his wife & two kids in Florida.