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)
System.Security (Espacio de nombres)
Exception
Ensamblado: mscorlib (en mscorlib.dll)
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.
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); } } }
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.
Referencia
SecurityException (Miembros)System.Security (Espacio de nombres)
Exception
Otros recursos
Controlar y generar excepciones
Contenido de la comunidad
Agregar