HOW TO: Create a Strong Named Assembly in Visual J# .NET (321917)



The information in this article applies to:

  • Microsoft Visual J# .NET (2002)
  • Microsoft Visual J# .NET (2003)
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1

This article was previously published under Q321917

SUMMARY

This step-by-step article describes how to create an assembly with a strong name in Visual J# .NET.

Assemblies can be assigned a cryptographic signature called a strong name. Strong names provide name uniqueness for the assembly and prevent someone from usurping the name of your assembly (name spoofing). If you are deploying an assembly that will be shared among many applications on the same computer, the assembly must be placed in the Global Assembly Cache (GAC). To be placed in the GAC, the assembly must have a strong name.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Visual Studio .NET
  • Visual J# .NET Beta 2
NOTE: To install Visual J# .NET, see the following Microsoft Developer Network (MSDN) Web site: back to the top

Create an Assembly with a Strong Name in Visual J#

  1. Use the Strong Name tool (Sn.exe) that is included with the .NET Framework Software Development Kit (SDK) to generate a cryptographic key pair. The following command uses the Strong Name tool to generate a new key pair and store the key pair in a file named TestKey.snk:

    sn -k TestKey.snk

  2. Add the proper custom attribute to your Visual J# source code for the compiler to emit the assembly with a strong name. The attribute that you use depends on whether the key pair that is used for the signing is contained in a file or in a key container in the Cryptographic Service Provider (CSP). For keys that are stored in a file, use the System.Reflection.AssemblyKeyFileAttribute attribute. For keys that are stored in the CSP, use the System.Reflection.AssemblyKeyNameAttribute attribute.

    The following code uses AssemblyKeyFileAttribute to specify the name of the file that contains the key pair:
    import System.Reflection.*;
    import System.Runtime.CompilerServices.*;
    /** @assembly AssemblyKeyFile("TestKey.snk") */ 
    					
    NOTE: You can add this code sample directly to your existing code module. Alternatively, in a Visual J# project, you can find the default project-generated Assemblyinfo.jsl file, which has an AssemblyKeyFileAttribute attribute with an empty-string key file name, and then fill in the key file name with the test key that you just created (TestKey.snk).
back to the top

REFERENCES

For more information about the Strong Name tool (Sn.exe), see the following Microsoft .NET Framework Tools Web site: For more information about the .NET Framework SDK, see the following Microsoft Web site: For more information about installing an assembly in the GAC, see the following Microsoft Web site: back to the top

Modification Type:MajorLast Reviewed:4/18/2006
Keywords:kbCrypt kbHOWTOmaster KB321917 kbAudDeveloper