. Net (C #): short-lived securitytreatassafe features

Source: Internet
Author: User

. NET 2.0 introduces a level-1 transparency model (but is only enforced within the Assembly). Transparent Code cannot access key code, even in the same assembly (or in the same type), the following code:

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system. text;

Using system. Security;

Using system. Security. permissions;

 

// Use transparency Model Level 1

[Assembly: securityrules (securityruleset. level1)]

[Assembly: securitycritical]

 

Namespace mgen

{

Class Program

{

// Transparent

Static void main (string [] ARGs)

{

Doo ();

}

 

// Key

[Securitycritical, securitytreatassafe]

Static void doo ()

{}

}

}

 

This code throws an exception:

Unhandled exception: system. methodaccessexception: attempt by security transpare

NT method 'mgen. program. Main (system. String []) 'to access security critical metho

D 'mgen. program. Doo () 'failed.

 

Obviously, transparent code cannot directly access key code.

In transparency Model Level 1, you can use the securitytreatassafe feature to mark the method and access it normally:

[Securitycritical, securitytreatassafe]

Static void doo ()

{}

However. net 4.0 introduced transparency Model Level 2, security model has undergone many changes, securitytreatassafe is also discarded, replaced by the securitysafecritical feature, directly marked as "reliable security code ".

 

The above code in. Net 4.0 should be complete:

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system. text;

Using system. Security;

Using system. Security. permissions;

 

//. Net 4.0 uses the transparency Model Level 2 by default

[Assembly: allowpartiallytrustedcallers]

 

Namespace mgen

{

Class Program

{

// Transparent

Static void main (string [] ARGs)

{

Doo ();

}

 

// Key to reliability

[Securitysafecritical]

Static void doo ()

{}

}

}

 

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.