Esta página foi útil?
Seus comentários sobre este conteúdo são importantes. Queremos saber sua opinião.
Comentários adicionais?
1500 caracteres restantes
Exportar (0) Imprimir
Expandir Tudo
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

Classe SecurityException

A exceção é lançada quando é detectado um erro de segurança.

Namespace:  System.Security
Assembly:  mscorlib (em mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class SecurityException : SystemException

SecurityException usa COR_E_SECURITY HRESULT, que tem o valor 0x8013150A.

Para obter uma lista dos valores de propriedade inicial de uma instância do SecurityException classe, consulte um determinado SecurityException construtor.

O exemplo de código a seguir mostra o uso de membros do SecurityException classe.

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]
        staticvoid 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.");
                thrownew SecurityException(sE.Message, sE.DenySetInstance, 
                    sE.PermitOnlySetInstance, sE.Method, sE.Demanded, 
                    (IPermission)sE.FirstPermissionThatFailed);
            }
        }
        void Display(string line)
        {
            Console.WriteLine(line);
        }
    }
}


System.Object
  System.Exception
    System.SystemException
      System.Security.SecurityException

Quaisquer membros static (Shared no Visual Basic) públicos deste tipo são thread-safe. Não há garantia de que qualquer membro de instância seja thread-safe.

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

o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.

.NET Framework

Compatível com: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatível com: 3.5, 2.0, 1.0

XNA Framework

Compatível com: , 1.0

Contribuições da comunidade

ADICIONAR
Mostrar:
© 2015 Microsoft