System.Security.Permissions ...


.NET Framework Class Library
CodeAccessSecurityAttribute Class

Specifies the base attribute class for code access security.

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

Visual Basic (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
Visual Basic (Usage)
Dim instance As CodeAccessSecurityAttribute
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple = true, 
    Inherited = false)]
public abstract class CodeAccessSecurityAttribute : SecurityAttribute
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Constructor|AttributeTargets::Method, AllowMultiple = true, 
    Inherited = false)]
public ref class CodeAccessSecurityAttribute abstract : public SecurityAttribute
JScript
public abstract class CodeAccessSecurityAttribute extends SecurityAttribute
Remarks

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.

Examples

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

Visual Basic
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
C#
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)]
    [Serializable]
    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.");
            }
            else
            {
                if (m_Name == null)
                    return new  NameIdPermission (PermissionState.None);

                return new  NameIdPermission (m_Name);
            }
        }
    }
}
Inheritance Hierarchy

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
Thread Safety

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

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.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

Greg Beech
Make sure your custom attribute is *not* in the same assembly as the code being secured
Otherwise you will possibly get a TypeLoadException when the attribute is instantiated (and you absolutely definitely will if you use any other types that are in the same assembly, e.g. a custom enumeration). See the following for details.

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297627

(Note that this issue was resolved as fixed, with the fix indicating that the docs should be updated with this info. It's a year later and they haven't been, so I've done it for them.)

Page view tracker