SecurityException Class

The exception that is thrown when a security error is detected.

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

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class SecurityException _
	Inherits SystemException
'Usage
Dim instance As SecurityException

SecurityException uses the HRESULT COR_E_SECURITY, which has the value 0x8013150A.

For a list of the initial property values for an instance of the SecurityException class, see a specific SecurityException constructor.

The following code example shows the use of members of the SecurityException class.

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

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

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, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft