CLR notes: 3. Shared Assembly forced assembly

Source: Internet
Author: User

3.1 Two Types of Assembly and two types of deployment
CLR has two types of Assembly: weak naming assembly and strong naming assembly. The difference is that strong naming Assembly uses the publisher's public key/private key pair to sign, uniquely identifies the publisher of an assembly. Weak naming Assembly can only be deployed privately. Strong naming assembly can be deployed globally or privately.

3.2 assign a strong name to the Assembly
A strongly-named Assembly includes four important attributes, with the unique identifier: An assembly without an extension, a version number, a language and cultural identifier, and a public key publickey. In addition, the publisher's private key is used for signature.
MyTypes, Version = 1.0.8123.0, Culture = neatral, PublicKeyToken = xxxxxxxxxxxxxx (Public Key tag)

MS uses the public/private key encryption technology, so that no two companies have the same public/private key pair (unless they share the public/private key pair ).

Use reflection to obtain the PublicKeyToken of a strongly-named assembly

To create a strongly-named assembly, follow these steps:
1. Generate a public/private key pair: Use the SN command. All the switches of this command are case sensitive.
SN-k MyCompany. keys
-- Here MyCompany. keys is the name of the created file.

2. Upgrade the original assembly to a strongly-named assembly.
Csc/keyfile: MyCompany. keys app. cs
-- Here, app. cs is a file that contains a list table. You cannot sign a file that does not contain a list table. C # the compiler will open MyCompany, sign the Assembly with the private key, and embed the public key in the list.

Sign a file with the private key: indicates that when a strong-naming assembly is generated, the FileDef list of the Assembly lists all contained parts and adds each file name to the list, the file content is hashed based on the private key, and the obtained hash value is stored together with the file name in FileDef. This hash value is called an RSA digital signature.

Finally, the generated PE32 file containing the list contains the RSA Digital Signature and public key.

Supplement 1: by default, the signature uses the SHA-1 algorithm, or other algorithms, which can be specified through the/algid switch of the AL command.

Supplement 2: You can also use the SN command to obtain a file containing only the public key and display it:
SN-p MyCompany. keys MyCompany. PublicKey
-- Here MyCompany. PublicKey is the name of the created public key file.
SN-pt MyCompany. PublicKey
-- Display public key and public key tags

Supplement 3: In IL, the Local corresponds to

Supplement 4: The public key is the last 8 bytes of the public key.
AssemblyRef stores the Public Key tag, and AssemblyDef stores the public key.

3.3 GAC Global Assembly Cache
GAC is generally structured in C: \ Windows \ Assembly and has many subdirectories.
Use the Windows Explorer shell extension to browse the GAC directory. This tool is included when the Framework is installed.
You cannot manually copy an assembly file to GAC by using the GACUtil command.
Only strongly-named assembly can be installed in GAC, And the Admin/PowerUser permission is required.

The advantage of GAC is that it can accommodate multiple versions of an assembly. Each version has its own directory. The disadvantage is that it violates the simple installation principle.

3.4 Reference a strongly-named assembly in the generated assembly
Chapter 1 describes the Directory Search sequence for incomplete paths during csc Compilation:
1. working directory (where the cs file to be compiled is located)
2. System Directory (csc.exe and clr dll)
3./lib switch the specified directory
4. directory specified by LIB System Variables

When the Framework is installed, two sets of copies of the. NET assembly will be installed, one in the compiler/CLR directory-to facilitate the generation of the Assembly, and the other in the GAC subdirectory-to facilitate loading them at runtime. It is not found in GAC during compilation.

Top 3.5 naming programs can prevent tampering
When a strongly-named assembly is installed to GAC, the system hashes the file content that contains the list and compares the value with the RSA Digital Signature embedded in the PE32 file. If the value is the same, then compare the content of other files (Also hash processing in comparison to RSA signature ). Once there is an inconsistency, it cannot be installed in GAC.

If the strongly-named assembly is installed in a directory other than GAC, the signature will be compared during loading.

3.6 delayed signature (partial signature) delayed signing
This function is used in the development stage.
Developers are allowed to generate an assembly using only the public key without the private key.
During compilation, a certain amount of space is reserved to store the RSA digital signature, and the file content is not hashed. CLR skips the hash value check. You can sign it later.
The procedure is as follows:
1. Generate the Assembly: csc/keyfile: MyCompany. PublicKey/delaysign: MyAssembly. cs
2. Skip the hash value check: SN.exe-Vr MyAssembly. dll
3. Prepare the private key and sign it again: SN.exe-R MyAssembly. dll MyCompany. PrivateKey
4. Re-delay Signature: SN.exe-Vu MyAssembly. dll

3.7 private deployment strong-name assembly
If a strongly-named assembly is not in GAC, it must be verified every time it is loaded, resulting in performance loss.
You can also design a local shared strongly-named assembly and specify the codeBase of the configuration file.

3.8 how does the runtime database parse type references?
In TypeRef, find the type reference record, find its strong signature, and locate the position of the Assembly: It will be found in the following three places:
1. The same file: can be found at compilation (early binding)
2. Different files, but the same assembly: In the FileRef table
3. Different files and assembly: load the referenced assembly and find

Note: AssemblyRef uses a file name without an extension to reference the assembly. When binding a program set, the system uses xx.dlland xx.exe to locate the file.
ModuleDef, ModuleRef, and FileDef use the file name and its extension to reference the file.

Note: In GAC, apart from the name, version, language culture, and public key, the CPU architecture is also required. In addition, the CPU architecture is used to search for the SDK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.