Export (0) Print
Expand All

CodeAccessSecurityAttribute Class

Specifies the base attribute class for code access security.

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

[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple = true, 
	Inherited = false)]
public abstract class CodeAccessSecurityAttribute : SecurityAttribute

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.

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);
            }
        }
    }
}

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

Community Additions

ADD
Show:
© 2014 Microsoft