Export (0) Print
Expand All
Expand Minimize

SecurityPermissionFlag Enumeration

Specifies access flags for the security permission object.

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

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration SecurityPermissionFlag
'Usage
Dim instance As SecurityPermissionFlag

/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute FlagsAttribute() */ 
public enum SecurityPermissionFlag
SerializableAttribute 
ComVisibleAttribute(true) 
FlagsAttribute 
public enum SecurityPermissionFlag

 Member nameDescription
AllFlagsThe unrestricted state of the permission. 
AssertionAbility to assert that all this code's callers have the requisite permission for the operation. 
BindingRedirectsPermission to perform explicit binding redirection in the application configuration file. This includes redirection of .NET Framework assemblies that have been unified as well as other assemblies found outside the .NET Framework. 
ControlAppDomainAbility to create and manipulate an AppDomain
ControlDomainPolicyAbility to specify domain policy. 
ControlEvidenceAbility to provide evidence, including the ability to alter the evidence provided by the common language runtime. 

This is a powerful permission that should only be granted to highly trusted code.

ControlPolicyAbility to view and modify policy. 

This is a powerful permission that should only be granted to highly trusted code.

ControlPrincipalAbility to manipulate the principal object. 
ControlThreadAbility to use certain advanced operations on threads. 
ExecutionPermission for the code to run. Without this permission, managed code will not be executed. 

This flag has no effect when used dynamically with stack modifiers such as Deny, Assert, and PermitOnly.

InfrastructurePermission to plug code into the common language runtime infrastructure, such as adding Remoting Context Sinks, Envoy Sinks and Dynamic Sinks. 
NoFlagsNo security access. 
RemotingConfigurationPermission to configure Remoting types and channels. 
SerializationFormatterAbility to provide serialization services. Used by serialization formatters. 
SkipVerificationAbility to skip verification of code in this assembly. Code that is unverifiable can be run if this permission is granted. 

This is a powerful permission that should be granted only to highly trusted code.

This flag has no effect when used dynamically with stack modifiers such as Deny, Assert, and PermitOnly.

UnmanagedCodeAbility to call unmanaged code. 

Since unmanaged code potentially allows other permissions to be bypassed, this is a dangerous permission that should only be granted to highly trusted code. It is used for such applications as calling native code using PInvoke or using COM interop.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

This enumeration is used by SecurityPermission.

Caution noteCaution:

Many of these flags are powerful and should only be granted to highly trusted code.

The following code example shows the use of the SecurityPermissionFlag enumeration to deny and demand security permissions.

' This sample demonstrates the use of the SecurityPermissionAttribute.
Imports System
Imports System.Security.Permissions
Imports System.Security
Imports Microsoft.VisualBasic



Class [MyClass]

    Public Shared Sub PermissionDemo()
        Try
            DenySecurityPermissions()
            DenyAllSecurityPermissions()
            DoNotDenySecurityPermissions()
        Catch e As Exception
            Console.WriteLine(e.Message.ToString())
        End Try
    End Sub 'PermissionDemo




    ' This method demonstrates the use of the SecurityPermissionAttribute to deny individual security permissions.
    ' Set the Assertion,UnmanagedCode, ControlAppDomain, ControlDomainPolicy, ontrolEvidence, 
    ' ControlPolicy, ControlPrincipal, ControlThread, Execution, Flags, Infrastructure, 
    ' RemotingConfiguration, SerializationFormatter, and SkipVerification properties.
    <SecurityPermissionAttribute(SecurityAction.Deny, Assertion:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, ControlAppDomain:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, ControlDomainPolicy:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, ControlEvidence:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, ControlPolicy:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, ControlPrincipal:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, ControlThread:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, Execution:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, Flags:=SecurityPermissionFlag.NoFlags), _
    SecurityPermissionAttribute(SecurityAction.Deny, Infrastructure:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, RemotingConfiguration:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, SerializationFormatter:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, SkipVerification:=True), _
    SecurityPermissionAttribute(SecurityAction.Deny, UnmanagedCode:=True)> _
    Public Shared Sub DenySecurityPermissions()
        Console.WriteLine("Executing DenySecurityPermissions.")
        Console.WriteLine("Denied all permissions individually.")
        TestSecurityPermissions()
    End Sub 'DenySecurityPermissions


    ' This method demonstrates the use of SecurityPermissionFlag.AllFlags to deny all security permissions.
    <SecurityPermissionAttribute(SecurityAction.Deny, Flags:=SecurityPermissionFlag.AllFlags)> _
    Public Shared Sub DenyAllSecurityPermissions()
        Console.WriteLine(ControlChars.Lf & "Executing DenyAllSecurityPermissions.")
        Console.WriteLine("Denied all permissions using SecurityPermissionFlag.AllFlags.")
        TestSecurityPermissions()
    End Sub 'DenyAllSecurityPermissions


    ' This method demonstrates the effect of not denying security permissions.
    Public Shared Sub DoNotDenySecurityPermissions()
        Console.WriteLine(ControlChars.Lf & "Executing DoNotDenySecurityPermissions.")
        Console.WriteLine("No permissions have been denied.")
        DemandSecurityPermissions()
    End Sub 'DoNotDenySecurityPermissions


    Public Shared Sub TestSecurityPermissions()
        Console.WriteLine(ControlChars.Lf & "Executing TestSecurityPermissions." & ControlChars.Lf)
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.Assertion)
            Console.WriteLine("Demanding SecurityPermissionFlag.Assertion")
            ' This demand should cause an exception.
            sp.Demand()
            ' The TestFailed method is called if an exception is not thrown.
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.Assertion failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlAppDomain)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlAppDomain")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlAppDomain failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlDomainPolicy)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlDomainPolicy")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlDomainPolicy failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlEvidence)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlEvidence")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlEvidence failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlPolicy)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPolicy")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlPolicy failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlPrincipal)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPrincipal")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlPrincipal failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlThread)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlThread")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlThread failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.Execution)
            Console.WriteLine("Demanding SecurityPermissionFlag.Execution")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.Execution failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.Infrastructure)
            Console.WriteLine("Demanding SecurityPermissionFlag.Infrastructure")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.Infrastructure failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.RemotingConfiguration)
            Console.WriteLine("Demanding SecurityPermissionFlag.RemotingConfiguration")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.RemotingConfiguration failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.SerializationFormatter)
            Console.WriteLine("Demanding SecurityPermissionFlag.SerializationFormatter")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.SerializationFormatter failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.SkipVerification)
            Console.WriteLine("Demanding SecurityPermissionFlag.SkipVerification")
            sp.Demand()
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.SkipVerification failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
            Console.WriteLine("Demanding SecurityPermissionFlag.UnmanagedCode")
            ' This demand should cause an exception.
            sp.Demand()
            ' The TestFailed method is called if an exception is not thrown.
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.UnmanagedCode failed: " & e.Message))
        End Try
    End Sub 'TestSecurityPermissions


    Public Shared Sub TestFailed()
        Console.WriteLine("In TestFailed method.")
        Console.WriteLine("Throwing an exception.")
        Throw New Exception()
    End Sub 'TestFailed

    Public Shared Sub DemandSecurityPermissions()
        Console.WriteLine(ControlChars.Lf & "Executing DemandSecurityPermissions." & ControlChars.Lf)
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.Assertion)
            Console.WriteLine("Demanding SecurityPermissionFlag.Assertion")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.Assertion succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.Assertion failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlAppDomain)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlAppDomain")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlAppDomain succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlAppDomain failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlDomainPolicy)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlDomainPolicy")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlDomainPolicy succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlDomainPolicy failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlEvidence)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlEvidence")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlEvidence succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlEvidence failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlPolicy)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPolicy")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPolicy succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlPolicy failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlPrincipal)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPrincipal")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPrincipal succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlPrincipal failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.ControlThread)
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlThread")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlThread succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.ControlThread failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.Execution)
            Console.WriteLine("Demanding SecurityPermissionFlag.Execution")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.Execution succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.Execution failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.Infrastructure)
            Console.WriteLine("Demanding SecurityPermissionFlag.Infrastructure")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.Infrastructure succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.Infrastructure failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.RemotingConfiguration)
            Console.WriteLine("Demanding SecurityPermissionFlag.RemotingConfiguration")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.RemotingConfiguration succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.RemotingConfiguration failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.SerializationFormatter)
            Console.WriteLine("Demanding SecurityPermissionFlag.SerializationFormatter")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.SerializationFormatter succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.SerializationFormatter failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.SkipVerification)
            Console.WriteLine("Demanding SecurityPermissionFlag.SkipVerification")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.SkipVerification succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.SkipVerification failed: " & e.Message))
        End Try
        Try
            Dim sp As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
            Console.WriteLine("Demanding SecurityPermissionFlag.UnmanagedCode")
            sp.Demand()
            Console.WriteLine("Demand for SecurityPermissionFlag.UnmanagedCode succeeded.")
        Catch e As Exception
            Console.WriteLine(("Demand for SecurityPermissionFlag.UnmanagedCode failed: " & e.Message))
        End Try
    End Sub 'DemandSecurityPermissions

    Overloads Shared Sub Main(ByVal args() As String)
        PermissionDemo()
    End Sub 'Main
End Class '[MyClass] 

// This sample demonstrates the use of the SecurityPermissionAttribute.

import System.*;
import System.Security.Permissions.*;
import System.Security.*;

class MyClass
{
    public static void PermissionDemo() 
    {
        try {
            DenySecurityPermissions();
            DenyAllSecurityPermissions();
            DoNotDenySecurityPermissions();
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message().ToString());
        }
    } //PermissionDemo   

    // This method demonstrates the use of the SecurityPermissionAttribute 
    // to deny individual security permissions.
    // Set the Assertion property.

    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        Assertion = true)
     */

    // Set the ControlAppDomain property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        ControlAppDomain = true)
     */
    
    // Set the ControlDomainPolicy property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        ControlDomainPolicy = true)
     */
    
    // Set the ControlEvidence property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        ControlEvidence = true)
     */
    
    // Set the ControlPolicy property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        ControlPolicy = true)
     */
    
    // Set the ControlPrincipal property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        ControlPrincipal = true)
     */
    
    // Set the ControlThread property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        ControlThread = true)
     */
    
    // Set the Execution property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        Execution = true)
     */
    
    // Set the Flags property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        Flags = SecurityPermissionFlag.NoFlags)
     */
    
    // Set the Infrastructure property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        Infrastructure = true)
     */
    
    // Set the RemotingConfiguration property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        RemotingConfiguration = true)
     */
    
    // Set the SerializationFormatter property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        SerializationFormatter = true)
     */
    
    // Set the SkipVerification property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        SkipVerification = true)
     */

    // Set the UnmanagedCode property.
    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        UnmanagedCode = true)
     */
    
    public static void DenySecurityPermissions() 
    {
        Console.WriteLine("Executing DenySecurityPermissions.");
        Console.WriteLine("Denied all permissions individually.");
        TestSecurityPermissions();
    } //DenySecurityPermissions  

    // This method demonstrates the use of SecurityPermissionFlag.AllFlags 
    // to deny all security permissions.

    /** @attribute SecurityPermissionAttribute(SecurityAction.Deny, 
        Flags = SecurityPermissionFlag.AllFlags)
     */
    
    public static void DenyAllSecurityPermissions() 
    {
        Console.WriteLine("\nExecuting DenyAllSecurityPermissions.");
        Console.WriteLine("Denied all permissions using " + 
        "SecurityPermissionFlag.AllFlags.");
        TestSecurityPermissions();
    } //DenyAllSecurityPermissions


    // This method demonstrates the effect of not denying security permissions.
    public static void DoNotDenySecurityPermissions() 
    {
        Console.WriteLine("\nExecuting DoNotDenySecurityPermissions.");
        Console.WriteLine("No permissions have been denied.");
        DemandSecurityPermissions();
    } //DoNotDenySecurityPermissions


    public static void TestSecurityPermissions() 
    {
        Console.WriteLine("\nExecuting TestSecurityPermissions.\n");
        try {
            SecurityPermission sp =  
                new SecurityPermission(SecurityPermissionFlag.Assertion);
            Console.WriteLine("Demanding SecurityPermissionFlag.Assertion");
            // This demand should cause an exception.
            sp.Demand();
            // The TestFailed method is called if an exception is not thrown.
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.Assertion failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
            new SecurityPermission(SecurityPermissionFlag.ControlAppDomain);
            Console.WriteLine("Demanding " + 
            "SecurityPermissionFlag.ControlAppDomain");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlAppDomain failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlDomainPolicy);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlDomainPolicy");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlDomainPolicy failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlEvidence);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlEvidence");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlEvidence failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlPolicy);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlPolicy");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlPolicy failed: " + 
            e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlPrincipal);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlPrincipal");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlPrincipal failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
            new SecurityPermission(SecurityPermissionFlag.ControlThread);
            Console.WriteLine("Demanding " 
            + "SecurityPermissionFlag.ControlThread");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlThread failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
            new SecurityPermission(SecurityPermissionFlag.Execution);
            Console.WriteLine("Demanding SecurityPermissionFlag.Execution");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.Execution failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
            new SecurityPermission(SecurityPermissionFlag.Infrastructure);
            Console.WriteLine("Demanding " + 
            "SecurityPermissionFlag.Infrastructure");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.Infrastructure failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.RemotingConfiguration);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.RemotingConfiguration");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) { 
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.RemotingConfiguration failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.SerializationFormatter);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.SerializationFormatter");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.SerializationFormatter failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.SkipVerification);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.SkipVerification");
            sp.Demand();
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.SkipVerification failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.UnmanagedCode);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.UnmanagedCode");
            // This demand should cause an exception.
            sp.Demand();
            // The TestFailed method is called if an exception is not thrown.
            TestFailed();
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.UnmanagedCode failed: " 
                + e.get_Message()));
        }
    } //TestSecurityPermissions

    public static void TestFailed() throws Exception
    {
        Console.WriteLine("In TestFailed method.");
        Console.WriteLine("Throwing an exception.");
        throw new Exception() ;
    } //TestFailed

    public static void DemandSecurityPermissions() 
    {
        Console.WriteLine("\nExecuting DemandSecurityPermissions.\n");
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.Assertion);
            Console.WriteLine("Demanding SecurityPermissionFlag.Assertion");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.Assertion succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.Assertion failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlAppDomain);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlAppDomain");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.ControlAppDomain succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlAppDomain failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlDomainPolicy);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlDomainPolicy");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.ControlDomainPolicy succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlDomainPolicy failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlEvidence);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlEvidence");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.ControlEvidence succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlEvidence failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlPolicy);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlPolicy");
            sp.Demand();
            Console.WriteLine("Demand " 
            + "for SecurityPermissionFlag.ControlPolicy succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlPolicy failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlPrincipal);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlPrincipal");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.ControlPrincipal succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlPrincipal failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.ControlThread);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.ControlThread");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.ControlThread succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.ControlThread failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.Execution);
            Console.WriteLine("Demanding SecurityPermissionFlag.Execution");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.Execution succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.Execution failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.Infrastructure);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.Infrastructure");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.Infrastructure succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.Infrastructure failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.RemotingConfiguration);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.RemotingConfiguration");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.RemotingConfiguration succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.RemotingConfiguration failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.SerializationFormatter);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.SerializationFormatter");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.SerializationFormatter succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.SerializationFormatter failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.SkipVerification);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.SkipVerification");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.SkipVerification succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.SkipVerification failed: " 
                + e.get_Message()));
        }
        try {
            SecurityPermission sp =  
                new SecurityPermission(
                SecurityPermissionFlag.UnmanagedCode);
            Console.WriteLine("Demanding " 
                + "SecurityPermissionFlag.UnmanagedCode");
            sp.Demand();
            Console.WriteLine("Demand for " 
                + "SecurityPermissionFlag.UnmanagedCode succeeded.");
        }
        catch(System.Exception e) {
            Console.WriteLine(("Demand for " 
                + "SecurityPermissionFlag.UnmanagedCode failed: " 
                + e.get_Message()));
        }
    } //DemandSecurityPermissions

    public static void main(String[] args)
    {
        PermissionDemo();
    } //main
} //MyClass 

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft