1.10 Verify That a Strong-Named Assembly Has Not Been Modified


1.10 Verify That a Strong-Named Assembly Has Not Been Modified

Problem

You need to verify that a strong-named assembly has not been modified after it was built.

Solution

Use the Strong Name tool (sn.exe) to verify the assembly's strong name .

Discussion

Whenever the .NET runtime loads a strong-named assembly, the runtime extracts the encrypted hash code that's embedded in the assembly and decrypts it with the public key, which is also embedded in the assembly. The runtime then calculates the hash code of the assembly manifest and compares it to the decrypted hash code. This verification process will identify if the assembly has changed after compilation.

If an executable assembly fails strong name verification, the runtime will display the dialog box shown in Figure 1.2. If code tries to load an assembly that fails verification, the runtime will throw a System.IO.FileLoadException with the message "Strong name validation failed."

click to expand
Figure 1.2: Error shown when you try to execute a strong-named assembly that has been modified.

As well as the generation and management of strong name keys (discussed in recipe 1.8), the Strong Name tool allows you to verify strong-named assemblies. To verify that the strong-named assembly HelloWorld.exe is unchanged, use the command sn -vf HelloWorld.exe . The -v switch requests the Strong Name tool to verify the strong name of the specified assembly, and the -f switch forces strong name verification even if it has been previously disabled for the specified assembly. (You can disable strong name verification for specific assemblies using the -Vr switch, as in sn -Vr HelloWorld.exe; see recipe 1.11 for details about why you would disable strong name verification.)

If the assembly passes strong name verification, you will see the following output:

 Microsoft (R) .NET Framework Strong Name Utility  Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. Assembly 'HelloWorld.exe' is valid 

However, if the assembly has been modified, you will see the message

 Microsoft (R) .NET Framework Strong Name Utility  Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. Failed to verify assembly -- Unable to format error message 8013141A 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net