Evaluar y enviar comentarios
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2005/.NET Framework 2.0

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
SecurityException (Clase)

La excepción que se produce cuando se detecta un error de seguridad.

Espacio de nombres: System.Security
Ensamblado: mscorlib (en mscorlib.dll)

Visual Basic (Declaración)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class SecurityException
    Inherits SystemException
Visual Basic (Uso)
Dim instance As SecurityException
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class SecurityException : SystemException
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class SecurityException : public SystemException
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class SecurityException extends SystemException
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public class SecurityException extends SystemException

La clase SecurityException utiliza HRESULT COR_E_SECURITY, que tiene el valor 0x8013150A.

Para obtener una lista con los valores de propiedad iniciales de una instancia de la clase SecurityException, vea un constructor SecurityException específico.

En el siguiente ejemplo de código se muestra el uso de miembros de la clase SecurityException.

Visual Basic
Imports System
Imports System.Data
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Policy
Imports System.Reflection
Imports System.Runtime.Serialization

<Assembly: KeyContainerPermissionAttribute(SecurityAction.RequestRefuse, _
    Flags:=KeyContainerPermissionFlags.Import)> 

Class EntryPoint

    <STAThread()> _
    Shared Sub Main()
        Dim eP As New EntryPoint()
        eP.RunCode()
        Console.WriteLine("Press the ENTER key to exit.")
        Console.Read()

    End Sub 'Main

    Sub RunCode()
        Try
            ' Deny a permission.
            Dim kCP1 As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Decrypt)
            kCP1.Deny()

            ' Demand the denied permission and display the 
            ' exception properties.
            Display("Demanding a denied permission. " & vbLf & vbLf)
            DemandDeniedPermission()
            Display("************************************************" & vbLf)
            CodeAccessPermission.RevertDeny()

            ' Demand the permission refused in the 
            ' assembly-level attribute.
            Display("Demanding a refused permission. " & vbLf & vbLf)
            DemandRefusedPermission()
            Display("************************************************" & vbLf)

            ' Demand the permission implicitly refused through a 
            ' PermitOnly attribute. Permit only the permission that 
            ' will cause the failure and the security permissions 
            ' necessary to display the results of the failure.
            Dim permitOnly As New PermissionSet(PermissionState.None)
            permitOnly.AddPermission( _
                New KeyContainerPermission(KeyContainerPermissionFlags.Import))
            permitOnly.AddPermission(New SecurityPermission( _
                SecurityPermissionFlag.ControlEvidence Or _ 
                SecurityPermissionFlag.ControlPolicy Or _
                SecurityPermissionFlag.SerializationFormatter))
            permitOnly.PermitOnly()
            Display( _
                "Demanding an implicitly refused permission. " & vbLf & vbLf)
            DemandPermitOnly()
        Catch sE As Exception
            Display("************************************************" & vbLf)
            Display("Displaying an exception using the ToString method: ")
            Display(sE.ToString())
        End Try
    End Sub 'RunCode

    Sub DemandDeniedPermission()
        Try
            Dim kCP As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Decrypt)
            kCP.Demand()
        Catch sE As SecurityException
            Display("The denied permission is: " & _
                CType(sE.DenySetInstance, PermissionSet).ToString())
            Display("The demanded permission is: " & sE.Demanded.ToString())
            Display("The security action is: " & sE.Action.ToString())
            Display("The method is: " & sE.Method.ToString())
            Display( _
                "The permission state at the time of the exception was: " & _
                sE.PermissionState)
            Display("The permission that failed was: " & _
                CType(sE.FirstPermissionThatFailed, _
                IPermission).ToXml().ToString())
            Display("The permission type is: " & sE.PermissionType.ToString())
            Display("Demonstrating the use of the GetObjectData method.")
            Dim si As New SerializationInfo( _
                GetType(EntryPoint), New FormatterConverter())
            sE.GetObjectData(si, _
                New StreamingContext(StreamingContextStates.All))
            Display("The FirstPermissionThatFailed from the " & _
                "call to GetObjectData is: ")
            Display(si.GetString("FirstPermissionThatFailed"))
        End Try
    End Sub 'DemandDeniedPermission

    Sub DemandRefusedPermission()
        Try
            Dim kCP As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Import)
            kCP.Demand()
        Catch sE As SecurityException
            Display("The refused permission set is: " & _
                sE.RefusedSet.ToString())
            Display("The exception message is: " & sE.Message)
            Display("The failed assembly is: " & _
                sE.FailedAssemblyInfo.EscapedCodeBase)
            Display("The granted set is: " & vbLf & sE.GrantedSet)
            Display("The permission that failed is: " & CType( _
                sE.FirstPermissionThatFailed, IPermission).ToXml().ToString())
            Display("The permission type is: " & sE.PermissionType.ToString())
            Display("The source is: " & sE.Source)
        End Try
    End Sub 'DemandRefusedPermission

    Sub DemandPermitOnly()
        Try
            Dim kCP As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Decrypt)
            kCP.Demand()
        Catch sE As SecurityException
            Display("The permitted permission is: " & _
                CType(sE.PermitOnlySetInstance, PermissionSet).ToString())
            Display("The demanded permission is: " & sE.Demanded.ToString())
            Display("The security action is: " & sE.Action.ToString())
            Display("The method is: " & sE.Method.ToString())
            Display("The permission state at the time of the exception was: " & _
                sE.PermissionState)
            Display("The permission that failed was: " & CType( _
                sE.FirstPermissionThatFailed, IPermission).ToXml().ToString())
            Display("The permission type is: " & sE.PermissionType.ToString())

            ' Demonstrate the SecurityException constructor by 
            ' throwing the exception again.
            Display("Rethrowing the exception thrown as a result of a " & _
                "PermitOnly security action.")
            Throw New SecurityException(sE.Message, sE.DenySetInstance, _
                sE.PermitOnlySetInstance, sE.Method, sE.Demanded, _
                CType(sE.FirstPermissionThatFailed, IPermission))
        End Try

    End Sub 'DemandPermitOnly

    Sub Display(ByVal line As String)

        Console.WriteLine(line)

    End Sub 'Display
End Class 'EntryPoint
C#
using System;
using System.Data;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Reflection;
using System.Runtime.Serialization;

[assembly: KeyContainerPermissionAttribute(SecurityAction.RequestRefuse, 
    Flags = KeyContainerPermissionFlags.Import)]
namespace TestForm
{
    class EntryPoint
    {
        [STAThread]
        static void Main()
        {
            EntryPoint eP = new EntryPoint();
            eP.RunCode();
            Console.WriteLine("Press the ENTER key to exit.");
            Console.Read();
        }
        void RunCode()
        {
            try
            {
                // Deny a permission.
                KeyContainerPermission kCP1 = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP1.Deny();

                // Demand the denied permission and display the 
                // exception properties.
                Display("Demanding a denied permission. \n\n");
                DemandDeniedPermission();
                Display("************************************************\n");
                CodeAccessPermission.RevertDeny();

                // Demand the permission refused in the 
                // assembly-level attribute.
                Display("Demanding a refused permission. \n\n");
                DemandRefusedPermission();
                Display("************************************************\n");

                // Demand the permission implicitly refused through a 
                // PermitOnly attribute. Permit only the permission that 
                // will cause the failure and the security permissions 
                // necessary to display the results of the failure.
                PermissionSet permitOnly = new PermissionSet(
                    PermissionState.None);
                permitOnly.AddPermission(new KeyContainerPermission(
                    KeyContainerPermissionFlags.Import));
                permitOnly.AddPermission(new SecurityPermission(
                    SecurityPermissionFlag.ControlEvidence |
                    SecurityPermissionFlag.ControlPolicy | 
                    SecurityPermissionFlag.SerializationFormatter));
                permitOnly.PermitOnly();
                Display("Demanding an implicitly refused permission. \n\n");
                DemandPermitOnly();
            }
            catch (Exception sE)
            {
                Display("************************************************\n");
                Display("Displaying an exception using the ToString method: ");
                Display(sE.ToString());
            }
        }

        void DemandDeniedPermission()
        {
            try
            {
                KeyContainerPermission kCP = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP.Demand();
            }
            catch (SecurityException sE)
            {
                Display("The denied permission is: " + 
                    ((PermissionSet)sE.DenySetInstance).ToString());
                Display("The demanded permission is: " + 
                    sE.Demanded.ToString());
                Display("The security action is: " + sE.Action.ToString());
                Display("The method is: " + sE.Method);
                Display(
                    "The permission state at the time of the exception was: " + 
                    sE.PermissionState);
                Display("The permission that failed was: " + 
                    (IPermission)sE.FirstPermissionThatFailed);
                Display("The permission type is: " + 
                    sE.PermissionType.ToString());
                Display("Demonstrating the use of the GetObjectData method.");
                SerializationInfo si = new SerializationInfo(
                    typeof(EntryPoint), new FormatterConverter());
                sE.GetObjectData(si, 
                    new StreamingContext(StreamingContextStates.All));
                Display("The FirstPermissionThatFailed from the " +
                    "call to GetObjectData is: ");
                Display(si.GetString("FirstPermissionThatFailed"));
            }
        }

        void DemandRefusedPermission()
        {
            try
            {
                KeyContainerPermission kCP = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Import);
                kCP.Demand();
            }
            catch (SecurityException sE)
            {
                Display("The refused permission set is: " + 
                    (sE.RefusedSet).ToString());
                Display("The exception message is: " + sE.Message);
                Display("The failed assembly is: " + 
                    sE.FailedAssemblyInfo.EscapedCodeBase);
                Display("The granted set is: \n" + sE.GrantedSet);
                Display("The permission that failed is: " + 
                    sE.FirstPermissionThatFailed);
                Display("The permission type is: " + 
                    sE.PermissionType.ToString());
                Display("The source is: " + sE.Source);
            }
        }

        void DemandPermitOnly()
        {
            try
            {
                KeyContainerPermission kCP = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP.Demand();
            }
            catch (SecurityException sE)
            {
                Display("The permitted permission is: " +
                    ((PermissionSet)sE.PermitOnlySetInstance).ToString());
                Display("The demanded permission is: " + 
                    sE.Demanded.ToString());
                Display("The security action is: " + sE.Action.ToString());
                Display("The method is: " + sE.Method.ToString());
                Display(
                    "The permission state at the time of the exception was: " +
                    sE.PermissionState);
                Display("The permission that failed was: " +
                    (IPermission)sE.FirstPermissionThatFailed);
                Display("The permission type is: " + 
                    sE.PermissionType.ToString());

                //Demonstrate the SecurityException constructor by 
                // throwing the exception again.
                Display("Rethrowing the exception thrown as a result of a " + 
                    "PermitOnly security action.");
                throw new SecurityException(sE.Message, sE.DenySetInstance, 
                    sE.PermitOnlySetInstance, sE.Method, sE.Demanded, 
                    (IPermission)sE.FirstPermissionThatFailed);
            }
        }
        void Display(string line)
        {
            Console.WriteLine(line);
        }
    }
}
C++
using namespace System;
using namespace System::Data;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Policy;
using namespace System::Reflection;
using namespace System::Runtime::Serialization;

namespace SecurityExceptionSample
{
    [assembly:KeyContainerPermissionAttribute(
        SecurityAction::RequestRefuse,
        Flags=KeyContainerPermissionFlags::Import)];
    ref class TestSecurityException
    {
    public:
        void MakeTest()
        {
            try
            {
                // Deny a permission.
                KeyContainerPermission^ keyContainerDecryptPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Decrypt);
                keyContainerDecryptPermission->Deny();
                
                // Demand the denied permission and display
                // the exception properties.
                Display("Demanding a denied permission. \n\n");
                DemandDeniedPermission();
                Display("*******************************************"
                    "**************\n");
                CodeAccessPermission::RevertDeny();
                
                // Demand the permission refused
                // in the assembly-level attribute.
                Display("Demanding a refused permission. \n\n");
                DemandRefusedPermission();
                Display("*******************************************"
                    "**************\n");
                
                // Demand the permission implicitly refused through a
                // PermitOnly attribute. Permit only the permission that
                // will cause the failure and the security permissions
                // necessary to display the results of the failure.
                PermissionSet^ permitOnlySet = gcnew PermissionSet(
                    PermissionState::None);
                permitOnlySet->AddPermission(gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Import));
                permitOnlySet->AddPermission(gcnew SecurityPermission(
                    SecurityPermissionFlag::ControlEvidence |
                    SecurityPermissionFlag::ControlPolicy |
                    SecurityPermissionFlag::SerializationFormatter));
                permitOnlySet->PermitOnly();
                Display("Demanding an implicitly refused permission. \n\n");
                DemandPermitOnly();
            }
            catch (SecurityException^ exception)
            {
                Display("*******************************************"
                    "**************\n");
                
                Display("Displaying an exception using the ToString "
                    "method: ");
                Display(exception->ToString());
            }

        }

    private:
        void DemandDeniedPermission()
        {
            try
            {
                KeyContainerPermission^ keyContainerDecryptPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Decrypt);
                keyContainerDecryptPermission->Demand();
            }
            catch (SecurityException^ exception)
            {
                Display("The denied permission is: {0}",
                    exception->DenySetInstance);

                Display("The demanded permission is: {0}",
                    exception->Demanded);

                Display("The security action is: {0}",
                    exception->Action);

                Display("The method is: {0}", exception->Method);

                Display("The permission state at the time "
                    "of the exception was: {0}",
                    exception->PermissionState);

                Display("The permission that failed was: {0}",
                    exception->FirstPermissionThatFailed);

                Display("The permission type is: {0}",
                    exception->PermissionType);

                Display("Demonstrating the use of the GetObjectData "
                    "method.");
                SerializationInfo^ entryPointSerializatonInfo =
                    gcnew SerializationInfo(TestSecurityException::typeid,
                    gcnew FormatterConverter);
                exception->GetObjectData(entryPointSerializatonInfo,
                    *gcnew StreamingContext(StreamingContextStates::All));
                Display("The FirstPermissionThatFailed from the call"
                    " to GetObjectData is: ");
                Display(entryPointSerializatonInfo->GetString(
                    "FirstPermissionThatFailed"));
            }
        }

        void DemandRefusedPermission()
        {
            try
            {
                KeyContainerPermission^ keyContainerImportPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Import);
                keyContainerImportPermission->Demand();
            }
            catch (SecurityException^ exception)
            {            
                Display("The refused permission set is: {0}",
                    exception->RefusedSet);

                Display("The exception message is: {0}",
                    exception->Message);

                Display("The failed assembly is: {0}",
                    exception->FailedAssemblyInfo->EscapedCodeBase);

                Display("The granted set is: \n{0}",
                    exception->GrantedSet);

                Display("The permission that failed is: {0}",
                    exception->FirstPermissionThatFailed);
                Display("The permission type is: {0}",
                    exception->PermissionType);

                Display("The source is: {0}", exception->Source);
            }
        }

        void DemandPermitOnly()
        {
            try
            {
                KeyContainerPermission^ keyContainerDecryptPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Decrypt);
                keyContainerDecryptPermission->Demand();
            }
            catch (SecurityException^ exception)
            {
                Display("The permitted permission is: {0}",
                    exception->PermitOnlySetInstance);
                Display("The demanded permission is: {0}",
                    exception->Demanded);
                Display("The security action is: {0}",
                    exception->Action);
                Display("The method is: {0}", exception->Method);
                Display("The permission state at the time of the "
                    "exception was: {0}", exception->PermissionState);
                Display("The permission that failed was: {0}",
                    exception->FirstPermissionThatFailed);
                Display("The permission type is: {0}",
                    exception->PermissionType);

                // Demonstrate the SecurityException constructor
                // by throwing the exception again.
                Display("Rethrowing the exception thrown as a "
                    "result of a PermitOnly security action.");
                throw gcnew SecurityException(exception->Message,
                    exception->DenySetInstance,
                    exception->PermitOnlySetInstance,
                    exception->Method, exception->Demanded,
                    exception->FirstPermissionThatFailed);
                }
        }

        void Display(String^ line)
        {
            Console::WriteLine(line);
        }

        void Display(String^ format, Object^ arg)
        {
            Console::WriteLine(format, arg);
        }
    };
}

using namespace SecurityExceptionSample;

int main()
{
    TestSecurityException^ test = gcnew TestSecurityException;
    test->MakeTest();
    Console::WriteLine("Press the enter key to exit.");
    Console::Read();
}
System.Object
   System.Exception
     System.SystemException
      System.Security.SecurityException
Los miembros estáticos públicos (Shared en Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2009 Microsoft Corporation. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker