A .NET assembly is “signed” if the developer compiled the assembly with the private key of a digital signature. When the system later loads the assembly, it verifies the assembly with the corresponding public key. Occasionally you may need to determine whether an assembly you have loaded has been signed.

If the .NET application or library you are building is signed, then all assemblies referenced by your application at compile time must also be signed. If you add to your signed application a reference to an unsigned assembly, you will receive this error when you compile your application:

Error 1 Assembly generation failed — Referenced assembly ‘XYZ’ does not have a strong name

However, if the application you are building is unsigned, or if you are loading an assembly at run-time, then it’s possible to load an unsigned assembly.

The following code demonstrates how to determine if a loaded .NET assembly is signed. In this example, “MyType” represents the name of any class defined in the assembly that you want to check:

Assembly asm = Assembly.GetAssembly( typeof( MyType ) );
if (asm != null)
{
    AssemblyName asmName = asm.GetName();
    byte[] key = asmName.GetPublicKey();
    bool isSignedAsm = key.Length > 0;
    Console.WriteLine( "IsSignedAssembly={0}", isSignedAsm );
}

See .NET Assembly FAQ – Part 3 – Strong Names and Signing for more information about signed assemblies.

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot