How to: Use Full Signing to Give a Dynamic Assembly a Strong Name

A dynamic assembly can be given a strong name using partial signing or full signing. For partial signing, the public key must be specified in the AssemblyName passed to the DefineDynamicAssembly method. The common language runtime allocates the space within the portable executable (PE) file for a strong name signature blob, but does not actually sign the assembly. The resulting assembly can be fully signed in a post-processing step using tools provided in the Windows Software Development Kit (SDK).

For full signing, a public/private key pair must be provided. These entities are usually stored in a file or disk or in a key container owned by a Crypto API Cryptographic Service Provider (CSP). Low security keys are often generated by software-based CSPs and exported to a file so they can be checked into source code management systems during project development. High security keys are often generated by hardware that usually helps prevent export of the keys for security reasons. Such key pairs can only be accessed indirectly through a key container. The strong name key pair is specified using the System.Reflection.StrongNameKeyPair class.

The following example demonstrates using full signing to give a dynamic assembly a strong name.

Example

Dim fs As New FileStream("SomeKeyPair.snk", FileMode.Open)
Dim kp As New StrongNameKeyPair(fs)
fs.Close()
Dim an As New AssemblyName()
an.KeyPair = kp
Dim myAppDomain As AppDomain = Thread.GetDomain()
Dim ab As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave)
FileStream fs = new FileStream("SomeKeyPair.snk", FileMode.Open);
StrongNameKeyPair kp = new StrongNameKeyPair(fs);
fs.Close();
AssemblyName an = new AssemblyName();
an.KeyPair = kp;
AppDomain appDomain = Thread.GetDomain();
AssemblyBuilder ab = appDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);

See Also

Concepts

Strong-Named Assemblies

Other Resources

Using Reflection Emit

Creating and Using Strong-Named Assemblies