Design and implementation of application framework ——. NET platform (10 licensing services. CodeAccessSecurityAttribute)

Source: Internet
Author: User
Tags assert

The following is reproduced from: http://blog.csdn.net/dongma_yifei/article/details/1533124

CodeAccessSecurityAttribute is the parent class of PrincipalPermissionAttribute and is a framework class that both the compiler and the CLR recognize.

CodeAccessPermission inherits from IPermission.


Authorization is done by the implementation class of the IPermission interface, which invokes the IPermission Demand () method when the CLR validates permissions.
The object of the IPermission interface class is generated by the CreatePermission () method of the CodeAccessSecurityAttribute implementation class object;
The CreatePermission () method uses the Factory mode, which allows the user to return a class object that implements the IPermission interface in the method.

CodeAccessSecurityAttribute and CodeAccessPermission are abstract classes that cannot be used to generate objects, and users must define their own inheritance classes to implement.


When the compiler discovers a method marked CodeAccessSecurityAttribute, there are several processing steps:
1. The compiler scans the source code to find out the characteristics of the CodeAccessSecurityAttribute type;
2. The compiler creates an attribute object and assigns a value to the attribute object using the property value specified in the attribute tag in the source code;
3. The compiler invokes the CreatePermission () method of the attribute object, creates a permission object, passes the attribute information to it, and returns the object to the compiler;
4. The compiler invokes the TOXML () method of the permission object to obtain a SecurityElement object;
5. The compiler converts the objects returned by the TOXML () method into XML data and stores the app's metadata.


During the execution of a program, when the CLR discovers the permission set information, it first creates a permission object with the type information in it, and then the CLR invokes the FromXml () Party of the Permission object

method, the CLR calls its demand () method when the property of the permission object is assigned to the XML data of the methods metadata.



[AttributeUsage (attributetargets.assembly |
AttributeTargets.Class |
attributetargets.struct |
Attributetargets.constructor |
AttributeTargets.Method,
Allowmultiple=true, Inherited=false)]
Public abstract class Codeaccesssecurityattribute:securityattribute
{
Constructors.
Internal CodeAccessSecurityAttribute ()
: Base ()
{
Nothing.
}
Public CodeAccessSecurityAttribute (SecurityAction action)
: Base (Action)
{
Nothing.
}

}; Class CodeAccessSecurityAttribute


public interface Ipermission:isecurityencodable
{
IPermission Copy ();

IPermission Intersect (IPermission target);

IPermission Union (IPermission target);

BOOL IsSubsetOf (IPermission target);

[DynamicSecurityMethodAttribute ()]
void Demand ();
}


Public abstract class Codeaccesspermission:ipermission
{
Constructor.
Protected CodeAccessPermission (){}

Assert permissions for the caller.
public void Assert ()
{
if (! Clrsecurity.assert (this, 1))
{
throw new SecurityException
(_ ("exception_securitynotgranted"));
}
}

Deny permissions to the caller.
public void Deny ()
{
Clrsecurity.deny (this, 1);
}

Convert this object into a string.
public override String ToString ()
{
Return ToXml (). ToString ();
}

Convert an XML value into a permissions value.
public abstract void FromXml (securityelement elem);

Convert This permissions object to an XML value.
Public abstract securityelement ToXml ();

Implement the IPermission interface.
Public abstract IPermission Copy ();
public void Demand ()
{
if (! Clrsecurity.demand (this, 1))
{
throw new SecurityException
(_ ("exception_securitynotgranted"));
}
}
Public abstract IPermission Intersect (IPermission target);
public abstract bool IsSubsetOf (IPermission target);
Public virtual IPermission Union (IPermission target)
{
return null;
}
}; Class CodeAccessPermission

Design and implementation of application framework ——. NET platform (10 licensing services. CodeAccessSecurityAttribute)

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.