RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Sign a .NET Assembly with a Strong Name Without Recompiling 

Signing a .NET assembly with a strong name is easy in Visual Studio. However, what if this is a 3rd party assembly and you don't have the source?

For me, I have an application that has a requirement that all assemblies are signed with a strong name. One of the assemblies I am using is RestSharp. I like to contribute to RestSharp and I didn't want to modify the project file to sign the assembly as I didn't want that to go back to the repository when I had some changes to contribute.

Not a problem. What I have is a batch file that disassembles the DLL to IL and then reassembles the IL back into a DLL and includes my key file. This way I get to keep the original DLL for projects that I don't need the assembly to have a strong name and a separate one that is signed with the strong name for the projects where I need that.

Here's what I did:
  1. In the Release build folder I created the following:
    • The key file (create using sn.exe -k MyPublicPrivateKeyFile.snk)
    • A subfolder to contain the signed assembly, mine is named "Signed"
    • A batch file (see below)
  2. Create a batch file in the Release folder named "SignAssembly.bat" with the following contents:
del .\Signed\RestSharp.* /F
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe" .\RestSharp.dll /out:.\Signed\RestSharp.il
"C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe" .\Signed\RestSharp.il /dll /key=.\RestSharp.snk /output=.\Signed\RestSharp.dll

pause
The first line deletes any previously signed assembly. Second line uses ILDASM to disassemble the DLL to IL in the "Signed" folder as RestSharp.il. The third line reassembles the IL into an assembly using ILASM and tells it to use the IL file and the key file. That's it.

Now, whenever I do a new release build, I just run the batch file and I have a signed copy to use, and I did it without changing the project file to do it.




                   



Leave a comment below.

Comments

  1. Jeff Ballard 4/24/2010 11:23 AM
    Gravatar
    Very cool, Ryan. Never had a need to do something like this, but good to know.

    Could you run the batch file as part of a post-build action so you don't forget to manually run it?
  2. Ryan Farley 4/24/2010 11:25 AM
    Gravatar
    Definitely could do it as a post build event. However, my goal was to not modify the project file. If I could add it to the post build event I could have just set to add the strong name normally. Also, if you didn't have source you wouldn't have a post build event either.

    However, if you do have source, you could add something like this to the post build event and then have both a signed *and* unsigned version of the DLL.
  3. John 1/9/2011 9:31 PM
    Gravatar
    Can this be incorporated into the VisualStudio build configuration using a batch file.

    Thanks
  4. Keylogger Mac 3/20/2011 10:33 PM
    Gravatar
    keylogger Mac
    Cool, good to know things like this.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.

Please add 8 and 3 and type the answer here:



Featured Product



 

News


Also see my CRM Developer blog

Connect:              


Sponsor

Sections