Search code examples
c#.net-assemblyregasmcsc

Is there a way to sign a .CS file which was created using Notepad and compiled using CSC command?


I am creating an ActiveX DLL assembly. I have followed this article. As the article has mentioned, I have created a .cs file (using notepad) and manually compiled it by pasting the file under \WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx and running csc /t:library AClass.cs.

At the next step, when I try to register this assembly, using regasm AClass.dll /tlb /codebase, it gives me a message saying this assembly needs to be signed. It does not throw any error. But, it forces me to enable 'Initialize unsigned ActiveX scripts' under IE settings for it to work.

Can you guide me on how to sign such an assembly... So that enabling the 'Initialize unsigned ActiveX scripts' is not necessary for all clients.

I am using Windows Server 2012 machine with VS2012..


Solution

  • OK, here is what I did to sign the assembly:- (thanks to both authors above who have answered the question, this is a summary of the method I have followed:)

    1) create cs file using notepad: make sure you add [assembly:AssemblyKeyFileAttribute("mynewkey.snk")] [assembly:AssemblyDelaySignAttribute(true)] above the namespace tag and add using System.Reflection

    2) go to c:\program files x86\micorsoft SDKs\windows\v8.0\bin\NETFX 4.0 Tools\sn -k mynewkey.snk

    3) go to C:\mysource> C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /t:library myclass.cs

    4) c:\program files x86\micorsoft SDKs\windows\v8.0\bin\NETFX 4.0 Tools\sn -R AClass.dll myneykey.snk

    5) regasm AClass.dll/tlb /codebase ----> (run this from 4.0 framework)

    DONE

    I have followed this msdn link but instead of following the manual for STEP 4 of the link, I have used :- sn -k mynewkey.snk --> this is to create a key file..

    Also, you can compile the file in Visual Studio.. So, no need to perform steps 1 to 4 of above because VS allows you to sign as well as compile the file. Step 5 must be followed to register the DLL..