(0) exportieren Drucken
Alle erweitern
Erweitern Minimieren
Dieser Artikel wurde noch nicht bewertet - Dieses Thema bewerten.

SecurityPermissionFlag-Enumeration

Aktualisiert: November 2007

Gibt Zugriffsflags für das Sicherheitsberechtigungsobjekt an.

Diese Enumeration verfügt über ein ComVisibleAttribute-Attribut, das die bitweise Kombination der Memberwerte zulässt.

Namespace:  System.Security.Permissions
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum SecurityPermissionFlag
/** @attribute SerializableAttribute */ 
/** @attribute ComVisibleAttribute(true) */
/** @attribute FlagsAttribute */
public enum SecurityPermissionFlag
public enum SecurityPermissionFlag
MembernameBeschreibung
NoFlagsKein Sicherheitszugriff.
AssertionDie Fähigkeit zu bestätigen, dass alle Aufrufer dieses Codes über die erforderliche Berechtigung für den Vorgang verfügen.
UnmanagedCodeDie Fähigkeit, nicht verwalteten Code aufzurufen.
SkipVerificationDie Fähigkeit, die Überprüfung von Code in dieser Assembly zu überspringen. Nicht überprüfbarer Code kann bei Erteilung dieser Berechtigung ausgeführt werden.
ExecutionBerechtigung für den auszuführenden Code. Ohne diese Berechtigung wird verwalteter Code nicht ausgeführt.
ControlThreadDie Fähigkeit zum Verwenden bestimmter erweiterter Vorgänge in Threads.
ControlEvidenceDie Fähigkeit zum Bereitstellen von Beweisen, einschließlich der Fähigkeit, die von der Common Language Runtime bereitgestellten Beweise zu ändern.
ControlPolicyDie Fähigkeit zum Anzeigen und Ändern von Richtlinien.
SerializationFormatterDie Fähigkeit zum Bereitstellen von Serialisierungsdiensten. Wird durch Serialisierungsformatierer verwendet.
ControlDomainPolicyDie Fähigkeit zum Angeben von Domänenrichtlinien.
ControlPrincipalDie Fähigkeit zum Verändern des Hauptobjekts.
ControlAppDomainDie Fähigkeit zum Erstellen und Bearbeiten von AppDomain.
RemotingConfigurationBerechtigung zum Konfigurieren von Remotetypen und Channels.
InfrastructureBerechtigung zum Einbinden von Code in die Infrastruktur der Common Language Runtime, z. B. für das Hinzufügen von Remotekontextempfängern, Botenempfängern und dynamischen Empfängern.
BindingRedirectsDie Berechtigung für die explizite Bindungsumleitung in der Anwendungskonfigurationsdatei. Dies umfasst die Umleitung von vereinheitlichten .NET Framework-Assemblys und von Assemblys außerhalb von .NET Framework.
AllFlagsDer uneingeschränkte Zustand der Berechtigung.

Diese Enumeration verfügt über ein FlagsAttribute-Attribut, das die bitweise Kombination der Memberwerte zulässt.

Diese Enumeration wird von SecurityPermission verwendet.

xc5yzfbx.alert_caution(de-de,VS.90).gifVorsicht:

Viele dieser Flags sind sehr weitreichend und sollten nur für sehr vertrauenswürdigen Code erteilt werden.

Im folgenden Codebeispiel wird veranschaulicht, wie die SecurityPermissionFlag-Enumeration zum Verweigern und Anfordern von Sicherheitsberechtigungen verwendet wird.

// This sample demonstrates the use of the SecurityPermissionAttribute.

using System;
using System.Security.Permissions;
using System.Security;


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



    }

    // This method demonstrates the use of the SecurityPermissionAttribute to deny individual security permissions.
    // Set the Assertion property.
    [SecurityPermissionAttribute(SecurityAction.Deny, Assertion = true)]
        // Set the ControlAppDomain property.
    [SecurityPermissionAttribute(SecurityAction.Deny, ControlAppDomain = true)]
        // Set the ControlDomainPolicy property.
    [SecurityPermissionAttribute(SecurityAction.Deny, ControlDomainPolicy = true)]
        // Set the ControlEvidence property.
    [SecurityPermissionAttribute(SecurityAction.Deny, ControlEvidence = true)]
        // Set the ControlPolicy property.
    [SecurityPermissionAttribute(SecurityAction.Deny, ControlPolicy = true)]
        // Set the ControlPrincipal property.
    [SecurityPermissionAttribute(SecurityAction.Deny, ControlPrincipal = true)]
        // Set the ControlThread property.
    [SecurityPermissionAttribute(SecurityAction.Deny, ControlThread = true)]
        // Set the Execution property.
    [SecurityPermissionAttribute(SecurityAction.Deny, Execution = true)]
        // Set the Flags property.
    [SecurityPermissionAttribute(SecurityAction.Deny, Flags = SecurityPermissionFlag.NoFlags)]
        // Set the Infrastructure property.
    [SecurityPermissionAttribute(SecurityAction.Deny, Infrastructure = true)]
        // Set the RemotingConfiguration property.
    [SecurityPermissionAttribute(SecurityAction.Deny, RemotingConfiguration = true)]
        // Set the SerializationFormatter property.
    [SecurityPermissionAttribute(SecurityAction.Deny, SerializationFormatter = true)]
        // Set the SkipVerification property.
    [SecurityPermissionAttribute(SecurityAction.Deny, SkipVerification = true)]
        // Set the UnmanagedCode property.
    [SecurityPermissionAttribute(SecurityAction.Deny, UnmanagedCode = true)]

    public static void DenySecurityPermissions()
    {
        Console.WriteLine("Executing DenySecurityPermissions.");
        Console.WriteLine("Denied all permissions individually.");
        TestSecurityPermissions();
    }

    // This method demonstrates the use of SecurityPermissionFlag.AllFlags to deny all security permissions.
    [SecurityPermissionAttribute(SecurityAction.Deny, Flags = SecurityPermissionFlag.AllFlags)]
    public static void DenyAllSecurityPermissions()
    {
        Console.WriteLine("\nExecuting DenyAllSecurityPermissions.");
        Console.WriteLine("Denied all permissions using SecurityPermissionFlag.AllFlags.");
        TestSecurityPermissions();
    }

    // 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();
    }

    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 (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.Assertion failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlAppDomain);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlAppDomain");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlAppDomain failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlDomainPolicy);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlDomainPolicy");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlDomainPolicy failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlEvidence);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlEvidence");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlEvidence failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlPolicy);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPolicy");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPolicy failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlPrincipal);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPrincipal");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPrincipal failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlThread);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlThread");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlThread failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.Execution);
            Console.WriteLine("Demanding SecurityPermissionFlag.Execution");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.Execution failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.Infrastructure);
            Console.WriteLine("Demanding SecurityPermissionFlag.Infrastructure");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.Infrastructure failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.RemotingConfiguration);
            Console.WriteLine("Demanding SecurityPermissionFlag.RemotingConfiguration");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.RemotingConfiguration failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
            Console.WriteLine("Demanding SecurityPermissionFlag.SerializationFormatter");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.SerializationFormatter failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.SkipVerification);
            Console.WriteLine("Demanding SecurityPermissionFlag.SkipVerification");
            sp.Demand();
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.SkipVerification failed: " + e.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 (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.UnmanagedCode failed: " + e.Message);
        }

    }

    public static void TestFailed()
    {
        Console.WriteLine("In TestFailed method.");
        Console.WriteLine("Throwing an exception.");
        throw new Exception();
    }
	
    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 (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.Assertion failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlAppDomain);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlAppDomain");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlAppDomain succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlAppDomain failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlDomainPolicy);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlDomainPolicy");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlDomainPolicy succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlDomainPolicy failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlEvidence);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlEvidence");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlEvidence succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlEvidence failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlPolicy);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPolicy");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPolicy succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPolicy failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlPrincipal);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlPrincipal");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPrincipal succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlPrincipal failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.ControlThread);
            Console.WriteLine("Demanding SecurityPermissionFlag.ControlThread");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlThread succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.ControlThread failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.Execution);
            Console.WriteLine("Demanding SecurityPermissionFlag.Execution");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.Execution succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.Execution failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.Infrastructure);
            Console.WriteLine("Demanding SecurityPermissionFlag.Infrastructure");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.Infrastructure succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.Infrastructure failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.RemotingConfiguration);
            Console.WriteLine("Demanding SecurityPermissionFlag.RemotingConfiguration");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.RemotingConfiguration succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.RemotingConfiguration failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
            Console.WriteLine("Demanding SecurityPermissionFlag.SerializationFormatter");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.SerializationFormatter succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.SerializationFormatter failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.SkipVerification);
            Console.WriteLine("Demanding SecurityPermissionFlag.SkipVerification");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.SkipVerification succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.SkipVerification failed: " + e.Message);
        }
        try
        {
            SecurityPermission sp =
                new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            Console.WriteLine("Demanding SecurityPermissionFlag.UnmanagedCode");
            sp.Demand();
            Console.WriteLine("Demand for SecurityPermissionFlag.UnmanagedCode succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Demand for SecurityPermissionFlag.UnmanagedCode failed: " + e.Message);
        }

    }

    static void Main(string[] args)
    {
        PermissionDemo();
    }

}


// 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 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

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0, 1.1, 1.0
Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)
Vielen Dank für Ihr Feedback.

Community-Beiträge

HINZUFÜGEN
Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?
Anzeigen:
© 2014 Microsoft. Alle Rechte vorbehalten.