CodeAccessSecurityAttribute 类



此 API 现已过时。

为代码访问安全性指定基特性类。Specifies the base attribute class for code access security.

public ref class CodeAccessSecurityAttribute abstract : System::Security::Permissions::SecurityAttribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Method | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)]
[System.Obsolete("CAS support is not available with Silverlight applications.")]
public abstract class CodeAccessSecurityAttribute : System.Security.Permissions.SecurityAttribute
type CodeAccessSecurityAttribute = class
    inherit SecurityAttribute
Public MustInherit Class CodeAccessSecurityAttribute
Inherits SecurityAttribute


下面的示例演示一个派生自 CodeAccessSecurityAttribute 类的权限特性。The following example shows a permission attribute derived from the CodeAccessSecurityAttribute class.

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Remoting;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Reflection;
using namespace MyPermission;

// Use the command line option '/keyfile' or appropriate project settings to sign this assembly.
public ref class NameIdPermissionAttribute: public CodeAccessSecurityAttribute
   String^ m_Name;
   bool m_unrestricted;

   NameIdPermissionAttribute( SecurityAction action )
      : CodeAccessSecurityAttribute( action )
      m_Name = nullptr;
      m_unrestricted = false;

   property String^ Name
      String^ get()
         return m_Name;

      void set( String^ value )
         m_Name = value;

   virtual IPermission^ CreatePermission() override
      if ( m_unrestricted )
         throw gcnew ArgumentException( "Unrestricted permissions not allowed in identity permissions." );
         if ( m_Name == nullptr )
                  return gcnew NameIdPermission( PermissionState::None );
         return gcnew NameIdPermission( m_Name );


using System;
using System.IO;
using System.Runtime.Remoting;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
using MyPermission;
// Use the command line option '/keyfile' or appropriate project settings to sign this assembly.
[assembly: System.Security.AllowPartiallyTrustedCallersAttribute ()]

namespace MyPermissionAttribute
    [AttributeUsage (AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
    sealed public class  NameIdPermissionAttribute : CodeAccessSecurityAttribute
        private String m_Name = null;
        private bool m_unrestricted = false;

        public  NameIdPermissionAttribute (SecurityAction action): base( action )

        public String Name
            get { return m_Name; }
            set { m_Name = value; }

        public override IPermission CreatePermission ()
            if (m_unrestricted)
                throw new ArgumentException ("Unrestricted permissions not allowed in identity permissions.");
                if (m_Name == null)
                    return new  NameIdPermission (PermissionState.None);

                return new  NameIdPermission (m_Name);
Imports System.IO
Imports System.Runtime.Remoting
Imports System.Security
Imports System.Security.Permissions
Imports System.Reflection
Imports MyPermission

' Use the command line option '/keyfile' or appropriate project settings to sign this assembly.
<Assembly: System.Security.AllowPartiallyTrustedCallersAttribute()> 
Namespace MyPermissionAttribute

    <AttributeUsage(AttributeTargets.All, AllowMultiple:=True, Inherited:=False)> Public NotInheritable Class NameIdPermissionAttribute
        Inherits CodeAccessSecurityAttribute
        Private m_Name As String = Nothing
        Private m_unrestricted As Boolean = False

        Public Sub New(ByVal action As SecurityAction)
        End Sub

        Public Property Name() As String
                Return m_name
            End Get
            Set(ByVal Value As String)
                m_name = Value
            End Set
        End Property

        Public Overrides Function CreatePermission() As IPermission
            If m_unrestricted Then
                Throw New ArgumentException("Unrestricted permissions not allowed in identity permissions.")
                If m_name Is Nothing Then
                    Return New NameIdPermission(PermissionState.None)
                End If
                Return New NameIdPermission(m_name)
            End If
        End Function 'CreatePermission
    End Class
End Namespace



不再支持部分受信任的代码。Partially trusted code is no longer supported. 此属性在 .NET Core 中不起作用。This attribute has no effect in .NET Core.

此特性类将 SecurityAction(例如,Demand)与自定义安全特性相关联。This attribute class associates a SecurityAction, for example, Demand, with a custom security attribute.

派生自 CodeAccessSecurityAttribute 的类型用于帮助限制对资源或安全操作的访问。The types that derive from CodeAccessSecurityAttribute are used to help restrict access to resources or securable operations.

安全特性声明的安全信息存储在属性目标的元数据中,由系统在运行时进行访问。The security information declared by a security attribute is stored in the metadata of the attribute target and is accessed by the system at run time. 安全特性仅用于声明性安全。Security attributes are used only for declarative security. 使用派生自 CodeAccessPermission 的相应权限类实现命令性安全。Use the corresponding permission class derived from CodeAccessPermission for imperative security.


从此类派生的所有权限属性都必须只有一个使用 SecurityAction 作为其唯一参数的构造函数。All permission attributes derived from this class must have only a single constructor that takes a SecurityAction as its only parameter.



用指定的 CodeAccessSecurityAttribute 初始化 SecurityAction 的新实例。Initializes a new instance of CodeAccessSecurityAttribute with the specified SecurityAction.



获取或设置安全性操作。Gets or sets a security action.

(继承自 SecurityAttribute)

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

获取或设置一个值,该值指示是否声明了对受该特性保护的资源有完全(无限制的)权限。Gets or sets a value indicating whether full (unrestricted) permission to the resource protected by the attribute is declared.

(继承自 SecurityAttribute)



当在派生类中重写时,创建一个权限对象,随后可将其序列化为二进制形式并连同 SecurityAction 长久存储在程序集的元数据中。When overridden in a derived class, creates a permission object that can then be serialized into binary form and persistently stored along with the SecurityAction in an assembly's metadata.

(继承自 SecurityAttribute)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)


_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)