HOWTO: Strongly name prebuilt assemblies

Wednesday, December 15, 2004 11:22 AM

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 /
  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 /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?

comments powered by Disqus