RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Sign a .NET Assembly with a Strong Name Without Recompiling 


qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbff4d4e02000000ad00000001000500
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.
  5. James William 8/5/2011 12:17 AM
    Gravatar
    Really a cool way to sign an assembly. Can you please guide how to incorporate this in Post Build script
  6. torai 9/8/2011 8:02 PM
    Gravatar
    Thanks for your post, it works fine for me.
  7. Peter 9/14/2011 6:29 AM
    Gravatar
    Hi!

    Cool batch file, but when I use this they say that some native components couldn't be added (I'm loading a native dll into the managed one)!

    Have you a solution for that?



    greets
  8. Kumanan Murugesan 9/19/2011 6:20 AM
    Gravatar
    Awesome small little post. While I know how to sign an assembly, I haven't signed a third party library. MSDN help wasn't that much useful. But your post solved my problem. Thanks a lot.

    Kumanan.
  9. Ruben 9/23/2011 5:34 AM
    Gravatar
    Very useful post! thanks!
  10. saranya 10/10/2011 7:37 AM
    Gravatar
    what is the procedure for displaying multiple table in a single grid view by selecting dropdown list...
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections