Friday, November 28, 2008

Strong Naming Assembly


Any code you have that uses this assembly, will need to be recompiled with a reference to the new assembly with the newer version.
Strong named assemblies can only reference other strong named assemblies.
If your product includes many assemblies (.NET DLL’s) and you want to ship only one of them as a hotfix, you will not be able to because your other assemblies will still refer to old version of the DLL. So either restrict to the same old version or use the .Net Framework configuration tool to forward the old version to the new version.
If you have classes that need to be serialized, the engine includes type information in the serialized data, that way it can be reconstituted later. If you the AssemblyFormat property is not set correctly, you could end up with a situation here you have stored some data, upgraded your application (maybe because of a bug fix), and then error when you try to load your serialized database (even though nothing in that particular class has changed).
Unless your assemblies need to be placed in the GAC, or run as COM+ component, or some other special case,strong-naming them probably is not necessary.
A strong name consists of the assembly’s identity—its simple text name, version number, and culture information (if provided)—plus a public key and a digital signature. It is generated from an assembly file using the corresponding private key. (The assembly file contains the assembly manifest, which contains the names and hashes of all the files that make up the assembly.)

Security Note: A strong-named assembly can only use types from other strong-named assemblies. Otherwise, the security is already compromised.
It’s pretty easy using Visual Studio .net 2005 IDE, but we will also talk about alternatives later.
Go to “Property Pages” of your project from the Solution Explorer.
Select the “Signing” Tab from the list of tabs on the left.
Select the check-box “Sign the Assembly”.
Select “New…” from “Choose a strong name key file” drop-down list.
Just finish the dialog by providing strong name key filename(new) and password(optional).
There you go!!
Alright, other alternatives are also mentioned here.
First of all, we need a cryptographic key pair to sign an assembly with a strong name.
This public and private cryptographic key pair is used during compilation to create a strong-named assembly. Microsoft has provided Strong Name Tool (sn.exe) to create this key pair in a file with “.snk” extension.
Open the command prompt and type the following command,
sn -k //e.g. sn -k mySgnKey.snk will create a keypair file named mySgnKey.snk
Delay Signing an Assembly:
If you intend to delay sign an assembly and you control the whole key pair (which is unlikely outside test scenarios), you can use the following commands to generate a key pair and then extract the public key from it into a separate file.
First Command: sn -k mySgnKey.snk
Second Command: sn -p mySgnKey.snk publicSgnKey.snk
Sign an Assembly with a Strong Name using Assembly Linker
al /out: /keyfile:
In this command, assembly name is the name of the assembly to sign with a strong name, module name is the name of the code module used to create the assembly, and file name is the name of the container or file that contains the key pair.
al /out:MyAssembly.dll Project.Module /keyfile:mySgnKey.snk
Signing an Assembly with a Strong Name using Assembly Attributes
C# usage: [assembly:AssemblyKeyFileAttribute(@"sgKey.snk")]
VB usage:
Note: When signing an assembly with a strong name, the Assembly Linker (Al.exe) looks for the key file relative to the current directory and to the output directory. When using command-line compilers, you can simply copy the key to the current directory containing your code modules.
How To Reference This Strongly Named Assembly
/reference:
In this command, compiler command is the compiler command for the language you are using, and assembly name is the name of the strong-named assembly being referenced. You can also use other compiler options, such as the /t:library option for creating a library assembly.
The following example creates an assembly called myAssembly.dll that references a strong-named assembly called myLibAssembly.dll from a code module called myAssembly.cs.
csc /t:library MyAssembly.cs /reference:MyProjectAssembly.dll
How To Make a Run-time Reference to this Strongly Named Assembly
We need to use the name of the assembly in following manner…
, , ,
C#usage: Assembly.Load(”myAssemby,Version=1.0.0.1,Culture=neutral,PublicKeyToken=7b53aa32d4fc18b1″);
VB usage: Assembly.Load(”myAssemby,Version=1.0.0.1,Culture=neutral,PublicKeyToken=7b53aa32d4fc18b1″)


No comments: