CodeAccessPermission 类

定义

注意

Code Access Security is not supported or honored by the runtime.

定义所有代码访问权限的基础结构。

public abstract class CodeAccessPermission : System.Security.IPermission, System.Security.IStackWalk
[System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public abstract class CodeAccessPermission : System.Security.IPermission, System.Security.IStackWalk
[System.Serializable]
public abstract class CodeAccessPermission : System.Security.IPermission, System.Security.IStackWalk
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class CodeAccessPermission : System.Security.IPermission, System.Security.IStackWalk
继承
CodeAccessPermission
派生
属性
实现

示例

下面的代码示例显示了派生自 CodeAccessPermission 类的权限。

//#define debug
// This custom permission is intended only for the purposes of illustration.
// The following code shows how to create a custom permission that inherits
// from CodeAccessPermission. The code implements all required overrides.
// A wildcard character ('*') is implemented for the Name property.
using System;
using System.Security;
using System.Security.Permissions;
using System.IO;
using System.Security.Policy;
using System.Collections;
using System.Text;

[assembly:System.Reflection.AssemblyKeyFile("Key.snk")]
[assembly:System.Security.AllowPartiallyTrustedCallersAttribute()]

namespace MyPermission
{
    [Serializable()] sealed public class   NameIdPermission : CodeAccessPermission, IUnrestrictedPermission
    {
        private String m_Name;
        private bool m_Unrestricted;

        public  NameIdPermission(String name)
        {
            m_Name = name;
        }

        public  NameIdPermission(PermissionState state)
        {
            if (state == PermissionState.None)
            {
                m_Name = "";
            }
            else
                if (state == PermissionState.Unrestricted)
            {
                throw new ArgumentException("Unrestricted state is not allowed for identity permissions.");
            }
            else
            {
                throw new ArgumentException("Invalid permission state.");
            }
        }

        public String Name
        {
            set{m_Name = value;}
            get{ return m_Name;}
        }
        public override IPermission Copy()
        {
            string name = m_Name;
            return new  NameIdPermission( name );
        }
        public bool IsUnrestricted()
        {
            // Always false, unrestricted state is not allowed.
            return m_Unrestricted;
        }

        private bool VerifyType(IPermission target)
        {
            return (target is  NameIdPermission);
        }
        public override bool IsSubsetOf(IPermission target)
        {
#if(debug)
            Console.WriteLine ("************* Entering IsSubsetOf *********************");
#endif
            if (target == null)
            {
                Console.WriteLine ("IsSubsetOf: target == null");
                return false;
            }
#if(debug)

            Console.WriteLine ("This is = " + (( NameIdPermission)this).Name);
            Console.WriteLine ("Target is " + (( NameIdPermission)target).m_Name);
#endif
            try
            {
                 NameIdPermission operand = ( NameIdPermission)target;

                // The following check for unrestricted permission is only included as an example for
                // permissions that allow the unrestricted state. It is of no value for this permission.
                if (true == operand.m_Unrestricted)
                {
                    return true;
                }
                else if (true == this.m_Unrestricted)
                {
                    return false;
                }

                if (this.m_Name != null)
                {
                    if (operand.m_Name == null) return false;

                    if (this.m_Name == "") return true;
                }

                if (this.m_Name.Equals (operand.m_Name))
                {
                    return true;
                }
                else
                {
                    // Check for wild card character '*'.
                    int i = operand.m_Name.LastIndexOf ("*");

                    if (i > 0)
                    {
                        string prefix = operand.m_Name.Substring (0, i);

                        if (this.m_Name.StartsWith (prefix))
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
            catch (InvalidCastException)
            {
                throw new ArgumentException (String.Format ("Argument_WrongType", this.GetType ().FullName));
            }
        }
        public override IPermission Intersect(IPermission target)
        {
            Console.WriteLine ("************* Entering Intersect *********************");
            if (target == null)
            {
                return null;
            }
#if(debug)
            Console.WriteLine ("This is = " + (( NameIdPermission)this).Name);
            Console.WriteLine ("Target is " + (( NameIdPermission)target).m_Name);
#endif
            if (!VerifyType(target))
            {
                throw new ArgumentException (String.Format ("Argument is wrong type.", this.GetType ().FullName));
            }

             NameIdPermission operand = ( NameIdPermission)target;

            if (operand.IsSubsetOf (this)) return operand.Copy ();
            else if (this.IsSubsetOf (operand)) return this.Copy ();
            else
                return null;
        }

        public override IPermission Union(IPermission target)
        {
#if(debug)
            Console.WriteLine ("************* Entering Union *********************");
#endif
            if (target == null)
            {
                return this;
            }
#if(debug)
            Console.WriteLine ("This is = " + (( NameIdPermission)this).Name);
            Console.WriteLine ("Target is " + (( NameIdPermission)target).m_Name);
#endif
            if (!VerifyType(target))
            {
                throw new ArgumentException (String.Format ("Argument_WrongType", this.GetType ().FullName));
            }

             NameIdPermission operand = ( NameIdPermission)target;

            if (operand.IsSubsetOf (this)) return this.Copy ();
            else if (this.IsSubsetOf (operand)) return operand.Copy ();
            else
                return null;
        }
        
        
        
        
       public override void FromXml(SecurityElement e)
        {
            // The following code for unrestricted permission is only included as an example for
            // permissions that allow the unrestricted state. It is of no value for this permission.
            String elUnrestricted = e.Attribute("Unrestricted");
            if (null != elUnrestricted)
            {
                m_Unrestricted = bool.Parse(elUnrestricted);
                return;
            }

            String elName = e.Attribute( "Name" );
            m_Name = elName == null ? null : elName;
        }
        public override SecurityElement ToXml()
        {
            // Use the SecurityElement class to encode the permission to XML.
            SecurityElement esd = new SecurityElement("IPermission");
            String name = typeof( NameIdPermission).AssemblyQualifiedName;
            esd.AddAttribute("class", name);
            esd.AddAttribute("version", "1.0");

            // The following code for unrestricted permission is only included as an example for
            // permissions that allow the unrestricted state. It is of no value for this permission.
            if (m_Unrestricted)
            {
                esd.AddAttribute("Unrestricted", true.ToString());
            }
            if (m_Name != null) esd.AddAttribute( "Name", m_Name );
            return esd;
        }
     }
}

注解

注意

代码访问安全性 (CAS) 已在所有版本的 .NET Framework 和 .NET 中弃用。 如果使用与 CAS 相关的 API,最新版本的 .NET 不会遵循 CAS 注释,并会生成错误。 开发人员应寻求用于完成安全任务的替代方法。

代码访问权限使用堆栈遍历来确保向代码的所有调用方授予权限。 如果权限对象为 null,则其处理方式与状态 PermissionState.None为 的权限对象相同。

调用堆栈通常表示为向下增长,因此调用堆栈中较高方法调用调用堆栈中较低的方法。

必须向 类的 CodeAccessPermission 继承者授予完全信任,才能作为扩展安全基础结构的权限正常运行。 若要确定继承者是完全受信任的,CodeAccessPermission请为 ControlEvidencetrue = 和 ControlPolicytrue = 发出 。InheritanceDemand

实施者说明

CodeAccessPermission继承时,还必须实现 IUnrestrictedPermission 接口。

必须重写以下CodeAccessPermission成员:Copy()、、Intersect(IPermission)IsSubsetOf(IPermission)ToXml()FromXml(SecurityElement)Union(IPermission)

还必须定义采用 作为其唯一参数的构造函数 PermissionState

必须将 特性 SerializableAttribute 应用于继承自 CodeAccessPermission的类。

构造函数

CodeAccessPermission()
已过时.

初始化 CodeAccessPermission 类的新实例。

方法

Assert()
已过时.

声明调用代码能够通过调用此方法的代码,访问受权限请求保护的资源,即使未对堆栈中处于较高位置的调用方授予访问该资源的权限。 使用 Assert() 会引起安全问题。

Copy()
已过时.

由派生类实现时,创建并返回当前权限对象的相同副本。

Demand()
已过时.

如果未对调用堆栈中处于较高位置的所有调用方授予当前实例所指定的权限,则在运行时强制 SecurityException

Deny()
已过时.
已过时.

防止处于调用堆栈较高位置的调用函数使用可以调用此方法来访问当前实例指定资源的代码。

Equals(Object)
已过时.

确定指定的 CodeAccessPermission 对象是否等于当前的 CodeAccessPermission

Equals(Object)
已过时.

确定指定对象是否等于当前对象。

(继承自 Object)
FromXml(SecurityElement)
已过时.

当在派生类中替代时,用 XML 编码重新构造具有指定状态的安全对象。

GetHashCode()
已过时.

获取 CodeAccessPermission 对象的哈希代码,此代码适合在哈希算法和数据结构(例如哈希表)中使用。

GetHashCode()
已过时.

作为默认哈希函数。

(继承自 Object)
GetType()
已过时.

获取当前实例的 Type

(继承自 Object)
Intersect(IPermission)
已过时.

通过派生类实现时,创建和返回是当前权限和指定权限的交集的权限。

IsSubsetOf(IPermission)
已过时.

通过派生类实现时,确定当前权限是否为指定权限的子集。

MemberwiseClone()
已过时.

创建当前 Object 的浅表副本。

(继承自 Object)
PermitOnly()
已过时.

防止处于调用堆栈较高位置的调用函数使用此代码,此代码将调用此方法来访问除当前实例指定的资源以外的所有资源。

RevertAll()
已过时.

导致当前帧以前所有的重写被删除且不再有效。

RevertAssert()
已过时.

导致当前帧以前所有的 Assert() 被删除且不再有效。

RevertDeny()
已过时.
已过时.

导致当前帧以前所有的 Deny() 被删除且不再有效。

RevertPermitOnly()
已过时.

导致当前帧以前所有的 PermitOnly() 被删除且不再有效。

ToString()
已过时.

创建并返回当前权限对象的字符串表示形式。

ToXml()
已过时.

当在派生类中重写时,创建安全对象及其当前状态的 XML 编码。

Union(IPermission)
已过时.

当在派生类中重写时,则创建属于当前权限与指定权限的并集的权限。

适用于