CodeAccessPermission Classe
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Cuidado
Code Access Security is not supported or honored by the runtime.
Define a estrutura subjacente de todas as permissões de acesso do código.
public ref class CodeAccessPermission abstract : System::Security::IPermission, System::Security::IStackWalk
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
type CodeAccessPermission = class
interface IPermission
interface ISecurityEncodable
interface IStackWalk
[<System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type CodeAccessPermission = class
interface IPermission
interface ISecurityEncodable
interface IStackWalk
[<System.Serializable>]
type CodeAccessPermission = class
interface IPermission
interface ISecurityEncodable
interface IStackWalk
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CodeAccessPermission = class
interface IPermission
interface ISecurityEncodable
interface IStackWalk
Public MustInherit Class CodeAccessPermission
Implements IPermission, IStackWalk
- Herança
-
CodeAccessPermission
- Derivado
- Atributos
- Implementações
Exemplos
O exemplo de código a seguir mostra uma permissão derivada da CodeAccessPermission classe .
//#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 namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::IO;
using namespace System::Security::Policy;
using namespace System::Collections;
using namespace System::Text;
[assembly:System::Reflection::AssemblyKeyFile("Key.snk")];
[assembly:System::Security::AllowPartiallyTrustedCallersAttribute];
[Serializable]
public ref class NameIdPermission: public CodeAccessPermission, public IUnrestrictedPermission
{
private:
String^ m_Name;
bool m_Unrestricted;
public:
NameIdPermission( String^ name )
{
m_Name = name;
}
NameIdPermission( PermissionState state )
{
if ( state == PermissionState::None )
{
m_Name = "";
}
else if ( state == PermissionState::Unrestricted )
{
throw gcnew ArgumentException( "Unrestricted state is not allowed for identity permissions." );
}
else
{
throw gcnew ArgumentException( "Invalid permission state." );
}
}
property String^ Name
{
String^ get()
{
return m_Name;
}
void set( String^ value )
{
m_Name = value;
}
}
public:
virtual IPermission^ Copy() override
{
String^ name = m_Name;
return gcnew NameIdPermission( name );
}
public:
virtual bool IsUnrestricted()
{
// Always false, unrestricted state is not allowed.
return m_Unrestricted;
}
private:
bool VerifyType( IPermission^ target )
{
return dynamic_cast<NameIdPermission^>(target) != nullptr;
}
public:
virtual bool IsSubsetOf( IPermission^ target ) override
{
#if ( debug )
Console::WriteLine( "************* Entering IsSubsetOf *********************" );
#endif
if ( target == nullptr )
{
Console::WriteLine( "IsSubsetOf: target == null" );
return false;
}
#if ( debug )
Console::WriteLine( "This is = {0}", ((NameIdPermission)this).Name );
Console::WriteLine( "Target is {0}", ((NameIdPermission)target).m_Name );
#endif
try
{
NameIdPermission^ operand = dynamic_cast<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 != nullptr )
{
if ( operand->m_Name == nullptr )
{
return false;
}
if ( this->m_Name->Equals( "" ) )
{
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 gcnew ArgumentException( String::Format( "Argument_WrongType", this->GetType()->FullName ) );
}
}
public:
virtual IPermission^ Intersect( IPermission^ target ) override
{
Console::WriteLine( "************* Entering Intersect *********************" );
if ( target == nullptr )
{
return nullptr;
}
#if ( debug )
Console::WriteLine( "This is = {0}", ((NameIdPermission)this).Name );
Console::WriteLine( "Target is {0}", ((NameIdPermission)target).m_Name );
#endif
if ( !VerifyType( target ) )
{
throw gcnew ArgumentException( String::Format( "Argument is wrong type.", this->GetType()->FullName ) );
}
NameIdPermission^ operand = dynamic_cast<NameIdPermission^>(target);
if ( operand->IsSubsetOf( this ) )
{
return operand->Copy();
}
else if ( this->IsSubsetOf( operand ) )
{
return this->Copy();
}
else
{
return nullptr;
}
}
public:
virtual IPermission^ Union( IPermission^ target ) override
{
#if ( debug )
Console::WriteLine( "************* Entering Union *********************" );
#endif
if ( target == nullptr )
{
return this;
}
#if ( debug )
Console::WriteLine( "This is = {0}", ((NameIdPermission)this).Name );
Console::WriteLine( "Target is {0}", ((NameIdPermission)target).m_Name );
#endif
if ( !VerifyType( target ) )
{
throw gcnew ArgumentException( String::Format( "Argument_WrongType", this->GetType()->FullName ) );
}
NameIdPermission^ operand = dynamic_cast<NameIdPermission^>(target);
if ( operand->IsSubsetOf( this ) )
{
return this->Copy();
}
else if ( this->IsSubsetOf( operand ) )
{
return operand->Copy();
}
else
{
return nullptr;
}
}
public:
virtual void FromXml( SecurityElement^ e ) override
{
// 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 ( nullptr != elUnrestricted )
{
m_Unrestricted = Boolean::Parse( elUnrestricted );
return;
}
String^ elName = e->Attribute("Name");
m_Name = elName == nullptr ? nullptr : elName;
}
public:
virtual SecurityElement^ ToXml() override
{
// Use the SecurityElement class to encode the permission to XML.
SecurityElement^ esd = gcnew SecurityElement( "IPermission" );
String^ name = NameIdPermission::typeid->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 != nullptr )
{
esd->AddAttribute( "Name", m_Name );
}
return esd;
}
};
//#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;
}
}
}
' 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.
Imports System.Security
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Policy
Imports System.Collections
<assembly: System.Reflection.AssemblyKeyFile("Key.snk")>
<assembly: System.Security.AllowPartiallyTrustedCallersAttribute()>
Namespace MyPermission
<Serializable()> _
Public NotInheritable Class NameIdPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
Private m_Name As String
Private m_Unrestricted As Boolean
Public Sub New(ByVal name As String)
m_name = name
End Sub
Public Sub New(ByVal state As PermissionState)
If state = PermissionState.None Then
m_name = ""
ElseIf state = PermissionState.Unrestricted Then
Throw New ArgumentException("Unrestricted state is not allowed for identity permissions.")
Else
Throw New ArgumentException("Invalid permission state.")
End If
End Sub
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 Copy() As IPermission
Dim name As String
name = m_name
Return New NameIdPermission(name)
End Function 'Copy
Public Function IsUnrestricted() As Boolean Implements IUnrestrictedPermission.IsUnrestricted
' Always false, unrestricted state is not allowed.
Return m_Unrestricted
End Function
Private Function VerifyType(ByVal target As IPermission) As Boolean
Return TypeOf target Is NameIdPermission
End Function 'VerifyType
Public Overrides Function IsSubsetOf(ByVal target As IPermission) As Boolean
#If (Debug) Then
Console.WriteLine("************* Entering IsSubsetOf *********************")
#End If
If target Is Nothing Then
Console.WriteLine("IsSubsetOf: target == null")
Return False
End If
#If (Debug) Then
Console.WriteLine(("This is = " + CType(Me, NameIdPermission).Name))
Console.WriteLine(("Target is " + CType(target, NameIdPermission).m_name))
#End If
Try
Dim operand As NameIdPermission = CType(target, NameIdPermission)
' 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 Then
Return True
ElseIf True = Me.m_Unrestricted Then
Return False
End If
If Not (Me.m_name Is Nothing) Then
If operand.m_name Is Nothing Then
Return False
End If
If Me.m_name = "" Then
Return True
End If
End If
If Me.m_name.Equals(operand.m_name) Then
Return True
Else
' Check for wild card character '*'.
Dim i As Integer = operand.m_name.LastIndexOf("*")
If i > 0 Then
Dim prefix As String = operand.m_name.Substring(0, i)
If Me.m_name.StartsWith(prefix) Then
Return True
End If
End If
End If
Return False
Catch
Throw New ArgumentException(String.Format("Argument_WrongType", Me.GetType().FullName))
End Try
End Function
Public Overrides Function Intersect(ByVal target As IPermission) As IPermission
Console.WriteLine("************* Entering Intersect *********************")
If target Is Nothing Then
Return Nothing
End If
#If (Debug) Then
Console.WriteLine(("This is = " + CType(Me, NameIdPermission).Name))
Console.WriteLine(("Target is " + CType(target, NameIdPermission).m_name))
#End If
If Not VerifyType(target) Then
Throw New ArgumentException(String.Format("Argument is wrong type.", Me.GetType().FullName))
End If
Dim operand As NameIdPermission = CType(target, NameIdPermission)
If operand.IsSubsetOf(Me) Then
Return operand.Copy()
ElseIf Me.IsSubsetOf(operand) Then
Return Me.Copy()
Else
Return Nothing
End If
End Function 'Intersect
Public Overrides Function Union(ByVal target As IPermission) As IPermission
#If (Debug) Then
Console.WriteLine("************* Entering Union *********************")
#End If
If target Is Nothing Then
Return Me
End If
#If (Debug) Then
Console.WriteLine(("This is = " + CType(Me, NameIdPermission).Name))
Console.WriteLine(("Target is " + CType(target, NameIdPermission).m_name))
#End If
If Not VerifyType(target) Then
Throw New ArgumentException(String.Format("Argument_WrongType", Me.GetType().FullName))
End If
Dim operand As NameIdPermission = CType(target, NameIdPermission)
If operand.IsSubsetOf(Me) Then
Return Me.Copy()
ElseIf Me.IsSubsetOf(operand) Then
Return operand.Copy()
Else
Return Nothing
End If
End Function 'Union
Public Overrides Sub FromXml(ByVal e As SecurityElement)
' 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.
Dim elUnrestricted As String = e.Attribute("Unrestricted")
If Nothing <> elUnrestricted Then
m_Unrestricted = Boolean.Parse(elUnrestricted)
Return
End If
Dim elName As String = e.Attribute("Name")
m_name = IIf(elName Is Nothing, Nothing, elName)
End Sub
Public Overrides Function ToXml() As SecurityElement
' Use the SecurityElement class to encode the permission to XML.
Dim esd As New SecurityElement("IPermission")
Dim name As String = GetType(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 Then
esd.AddAttribute("Unrestricted", True.ToString())
End If
If Not (m_Name Is Nothing) Then
esd.AddAttribute("Name", m_Name)
End If
Return esd
End Function 'ToXml
End Class
End Namespace
Comentários
Cuidado
O CAS (Segurança de Acesso do Código) foi preterido em todas as versões do .NET Framework e do .NET. As versões recentes do .NET não aceitam anotações de CAS e produzem erros caso as APIs relacionadas ao CAS sejam usadas. Os desenvolvedores devem buscar meios alternativos de realizar tarefas de segurança.
As permissões de acesso ao código usam uma caminhada de pilha para garantir que todos os chamadores do código tenham recebido uma permissão. Se um objeto de permissão for null
, ele será tratado da mesma forma que um objeto de permissão com o estado PermissionState.None.
Normalmente, a pilha de chamadas é representada como crescente, de modo que os métodos mais altos na pilha de chamadas chamem métodos mais baixos na pilha de chamadas.
Os herdadores da CodeAccessPermission classe devem receber confiança total para funcionar corretamente como permissões que estendem a infraestrutura de segurança. Para determinar que os herdadores são totalmente confiáveis, CodeAccessPermission emite um InheritanceDemand para ControlEvidence = true
e ControlPolicy = true
.
Notas aos Implementadores
Ao herdar de CodeAccessPermission, você também deve implementar a IUnrestrictedPermission interface .
Os seguintes CodeAccessPermission membros devem ser substituídos: Copy(), Intersect(IPermission), IsSubsetOf(IPermission), ToXml(), FromXml(SecurityElement)e Union(IPermission).
Você também deve definir um construtor que usa um PermissionState como seu único parâmetro.
Você deve aplicar o SerializableAttribute atributo a uma classe que herda de CodeAccessPermission.
Construtores
CodeAccessPermission() |
Obsoleto.
Inicializa uma nova instância da classe CodeAccessPermission. |
Métodos
Assert() |
Obsoleto.
Declara que o código de chamada pode acessar o recurso protegido por uma demanda de permissão através do código que chama este método, mesmo que os chamadores na pilha não tenham recebido permissão para acessar o recurso. Usar Assert() pode criar problemas de segurança. |
Copy() |
Obsoleto.
Quando implementada por uma classe derivada, cria e retorna uma cópia idêntica do objeto de permissão atual. |
Demand() |
Obsoleto.
Força um SecurityException no tempo de execução se todos os chamadores no topo da pilha de chamadas não receberam as permissões especificadas pela instância atual. |
Deny() |
Obsoleto.
Obsoleto.
Impede que os chamadores no nível superior da pilha de chamadas usem o código que chama esse método para acessar o recurso especificado pela instância atual. |
Equals(Object) |
Obsoleto.
Determina se o objeto CodeAccessPermission especificado é igual ao CodeAccessPermission atual. |
Equals(Object) |
Obsoleto.
Determina se o objeto especificado é igual ao objeto atual. (Herdado de Object) |
FromXml(SecurityElement) |
Obsoleto.
Quando substituído em uma classe derivada, reconstrói um objeto de segurança com um estado especificado de uma codificação XML. |
GetHashCode() |
Obsoleto.
Obtém um código hash para o objeto CodeAccessPermission atual, adequado para algoritmos de hash e estruturas de dados, tais como uma tabela de hash. |
GetHashCode() |
Obsoleto.
Serve como a função de hash padrão. (Herdado de Object) |
GetType() |
Obsoleto.
Obtém o Type da instância atual. (Herdado de Object) |
Intersect(IPermission) |
Obsoleto.
Quando implementada por uma classe derivada, cria e retorna uma permissão que é a interseção de permissão atual e da permissão especificada. |
IsSubsetOf(IPermission) |
Obsoleto.
Quando implementado em uma classe derivada, determina se a permissão atual é um subconjunto da permissão especificada. |
MemberwiseClone() |
Obsoleto.
Cria uma cópia superficial do Object atual. (Herdado de Object) |
PermitOnly() |
Obsoleto.
Impede que os chamadores no topo da pilha de chamadas usem o código que chama esse método para acessar todos os recursos, com exceção do recurso especificado pela instância atual. |
RevertAll() |
Obsoleto.
Faz com que todas as substituições anteriores para o quadro atual sejam removidas e não estejam mais em vigor. |
RevertAssert() |
Obsoleto.
Faz com que um Assert() anterior do quadro atual seja removido e não entre mais em vigor. |
RevertDeny() |
Obsoleto.
Obsoleto.
Faz com que um Deny() anterior do quadro atual seja removido e não entre mais em vigor. |
RevertPermitOnly() |
Obsoleto.
Faz com que um PermitOnly() anterior do quadro atual seja removido e não entre mais em vigor. |
ToString() |
Obsoleto.
Cria e retorna uma representação de cadeia de caracteres do objeto de permissão atual. |
ToXml() |
Obsoleto.
Quando substituído em uma classe derivada, cria uma codificação XML do objeto de segurança e seu estado atual. |
Union(IPermission) |
Obsoleto.
Quando substituído em uma classe derivada, cria uma permissão que é a união entre a permissão atual e a permissão especificada. |