This documentation is archived and is not being maintained.

CodeAccessSecurityAttribute Class

Specifies the base attribute class for code access security.

Namespace:  System.Security.Permissions
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Constructor Or AttributeTargets.Method, AllowMultiple := True,  _
	Inherited := False)> _
Public MustInherit Class CodeAccessSecurityAttribute _
	Inherits SecurityAttribute
'Usage
Dim instance As CodeAccessSecurityAttribute

This attribute class associates a SecurityAction, for example, Demand, with a custom security attribute.

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. Use the corresponding permission class derived from CodeAccessPermission for imperative security.

Notes to Inheritors:

All permission attributes derived from this class must have only a single constructor that takes a SecurityAction as its only parameter.

The following example shows a permission attribute derived from the CodeAccessSecurityAttribute class.

Imports System
Imports System.IO
Imports System.Runtime.Remoting
Imports System.Security
Imports System.Security.Permissions
Imports System.Reflection
Imports MyPermission
Imports Microsoft.VisualBasic


' 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)
            MyBase.New(action)
        End Sub 'New 


        Public Property Name() As String 
            Get 
                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.")
            Else 
                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 ' NameIdPermissionAttribute
End Namespace

System.Object
  System.Attribute
    System.Security.Permissions.SecurityAttribute
      System.Security.Permissions.CodeAccessSecurityAttribute
        System.Configuration.ConfigurationPermissionAttribute
        System.Data.Common.DBDataPermissionAttribute
        System.Data.OracleClient.OraclePermissionAttribute
        System.Diagnostics.EventLogPermissionAttribute
        System.Diagnostics.PerformanceCounterPermissionAttribute
        System.DirectoryServices.DirectoryServicesPermissionAttribute
        System.Drawing.Printing.PrintingPermissionAttribute
        System.Messaging.MessageQueuePermissionAttribute
        System.Net.DnsPermissionAttribute
        System.Net.Mail.SmtpPermissionAttribute
        System.Net.NetworkInformation.NetworkInformationPermissionAttribute
        System.Net.PeerToPeer.Collaboration.PeerCollaborationPermissionAttribute
        System.Net.PeerToPeer.PnrpPermissionAttribute
        System.Net.SocketPermissionAttribute
        System.Net.WebPermissionAttribute
        System.Security.Permissions.DataProtectionPermissionAttribute
        System.Security.Permissions.EnvironmentPermissionAttribute
        System.Security.Permissions.FileDialogPermissionAttribute
        System.Security.Permissions.FileIOPermissionAttribute
        System.Security.Permissions.GacIdentityPermissionAttribute
        System.Security.Permissions.HostProtectionAttribute
        System.Security.Permissions.IsolatedStoragePermissionAttribute
        System.Security.Permissions.KeyContainerPermissionAttribute
        System.Security.Permissions.MediaPermissionAttribute
        System.Security.Permissions.PermissionSetAttribute
        System.Security.Permissions.PrincipalPermissionAttribute
        System.Security.Permissions.PublisherIdentityPermissionAttribute
        System.Security.Permissions.ReflectionPermissionAttribute
        System.Security.Permissions.RegistryPermissionAttribute
        System.Security.Permissions.SecurityPermissionAttribute
        System.Security.Permissions.SiteIdentityPermissionAttribute
        System.Security.Permissions.StorePermissionAttribute
        System.Security.Permissions.StrongNameIdentityPermissionAttribute
        System.Security.Permissions.UIPermissionAttribute
        System.Security.Permissions.UrlIdentityPermissionAttribute
        System.Security.Permissions.WebBrowserPermissionAttribute
        System.Security.Permissions.ZoneIdentityPermissionAttribute
        System.ServiceProcess.ServiceControllerPermissionAttribute
        System.Transactions.DistributedTransactionPermissionAttribute
        System.Web.AspNetHostingPermissionAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: