次の方法で共有


ZoneIdentityPermission クラス

コードのダウンロード元のゾーンに対して ID アクセス許可を定義します。このクラスは継承できません。

この型のすべてのメンバの一覧については、ZoneIdentityPermission メンバ を参照してください。

System.Object
   System.Security.CodeAccessPermission
      System.Security.Permissions.ZoneIdentityPermission

<Serializable>
NotInheritable Public Class ZoneIdentityPermission   Inherits CodeAccessPermission
[C#]
[Serializable]
public sealed class ZoneIdentityPermission : CodeAccessPermission
[C++]
[Serializable]
public __gc __sealed class ZoneIdentityPermission : public   CodeAccessPermission
[JScript]
public
   Serializable
class ZoneIdentityPermission extends CodeAccessPermission

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このアクセス許可は、呼び出しコードが特定のゾーンからダウンロードされたかどうかを判断できます。ゾーンは、Microsoft Internet Explorer のオプションに従って構成され、Internet Explorer の IInternetSecurityManager および関連する API によって URL から割り当てられます。アクセス許可では、正確なゾーンの一致だけを定義します。つまり、URL は 1 つのゾーンだけに属することができます。

  • ローカル イントラネット ゾーン: ローカル イントラネット ゾーンは、会社のイントラネットにあるコンテンツで使用します。サーバーは、会社のファイアウォール内にあるため、イントラネットのコンテンツにはより高いレベルの信頼を割り当てます。
  • 信頼されたサイト ゾーン: 信頼されたサイト ゾーンは、インターネット上の他のサイトよりも評判が高く信頼できると見なされた Web サイトにあるコンテンツで使用されます。このゾーンを使用して、固有なインターネット サイトにより高いレベルの信頼を割り当てることができます。これらの信頼されている Web サイトの URL は、ユーザーがこのゾーンに割り当てる必要があります。既定では、信頼されたサイト ゾーンのサイトは、インターネット ゾーンのサイトより信頼性は低くなります。このゾーンに属するサイトに、より高いレベルの信頼を付与する場合、ユーザーまたは会社はこのゾーンに付与する信頼のレベルを変更する必要があります。
  • インターネット ゾーン: インターネット ゾーンは、別のゾーンに属さないインターネット上の Web サイトで使用します。既定の設定では、これらのサイトからダウンロードされたコードのアクセス権は、ユーザーのコンピュータのリソースに対して最小限になります。他のゾーンに割り当てられていない Web サイトは、自動的にこのゾーンに割り当てられます。
  • 制限付きサイト ゾーン: 制限付きサイト ゾーンは、ダウンロードした場合に問題が発生する可能性がある、または以前に発生したことがあるコンテンツを持つ Web サイトで使用します。このゾーンは、これらのサイトからダウンロードしたコードが、ユーザーのコンピュータで実行されることを禁止するために使用できます。これらの信頼されていない Web サイトは、ユーザーがこのゾーンに割り当てる必要があります。
  • ローカル コンピュータ ゾーン: ローカル コンピュータ ゾーンは、ユーザーのコンピュータに存在するコンテンツが使用する暗黙のゾーンです。ローカル システムに Internet Explorer がキャッシュしたコンテンツを除き、ユーザーのコンピュータにあるコンテンツは高い信頼レベルで処理されます。

使用例

 
' This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
' of the ZoneIdentityPermission class.
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports Microsoft.VisualBasic

<Assembly: CLSCompliant(True)> 

Module Module1

    Public Class ZoneIdentityDemo

        ' IsSubsetOf determines whether the current permission is a subset of the specified permission.
        Private Function IsSubsetOfDemo() As Boolean
            Dim returnValue As Boolean = True
            Dim zone1, zone2 As SecurityZone
            Dim zoneIdPerm1, zoneIdPerm2 As ZoneIdentityPermission
            Dim zoneGen1 As New zoneGenerator()
            Dim zoneGen2 As New zoneGenerator()
            zoneGen1.ResetIndex()
            While zoneGen1.CreateZone(zoneIdPerm1, zone1)
                If zoneIdPerm1 Is Nothing Then
                    GoTo ContinueWhile1
                End If
                zoneGen2.ResetIndex()
                Console.WriteLine("********************************************************" & ControlChars.Cr)

                While zoneGen2.CreateZone(zoneIdPerm2, zone2)
                    If zoneIdPerm2 Is Nothing Then
                        GoTo ContinueWhile2
                    End If
                    Try
                        If zoneIdPerm1.IsSubsetOf(zoneIdPerm2) Then
                            Console.WriteLine((zoneIdPerm1.SecurityZone.ToString() & _
                            " is a subset of " & zoneIdPerm2.SecurityZone.ToString()))
                        Else
                            Console.WriteLine((zoneIdPerm1.SecurityZone.ToString() & _
                            " is not a subset of " & zoneIdPerm2.SecurityZone.ToString()))
                        End If
                    Catch e As Exception
                        Console.WriteLine(("An exception was thrown for subset :" & _
                        zoneIdPerm1.ToString() & ControlChars.Cr & zoneIdPerm2.ToString() & _
                        ControlChars.Cr & e.ToString()))
                        returnValue = False
                    End Try
ContinueWhile2:
                End While
ContinueWhile1:
            End While
            Return returnValue
        End Function 'IsSubsetOfDemo

        ' Union creates a new permission that is the union of the current permission and the specified permission.
        Private Function UnionDemo() As Boolean
            Dim returnValue As Boolean = True
            Dim zone1, zone2 As SecurityZone
            Dim zoneIdPerm1, zoneIdPerm2 As ZoneIdentityPermission
            Dim zoneIdPerm3 As IPermission
            Dim zoneGen1 As New zoneGenerator()
            Dim zoneGen2 As New zoneGenerator()
            zoneGen1.ResetIndex()
            While zoneGen1.CreateZone(zoneIdPerm1, zone1)
                If zoneIdPerm1 Is Nothing Then
                    GoTo ContinueWhile1
                End If
                Console.WriteLine("**********************************************************" & ControlChars.Cr)
                zoneGen2.ResetIndex()

                While zoneGen2.CreateZone(zoneIdPerm2, zone2)
                    If zoneIdPerm2 Is Nothing Then
                        GoTo ContinueWhile2
                    End If
                    zoneIdPerm3 = CType(zoneIdPerm1.Union(zoneIdPerm2), ZoneIdentityPermission)
                    zoneIdPerm3 = zoneIdPerm1.Union(zoneIdPerm2)

                    If zoneIdPerm3 Is Nothing Then
                        Console.WriteLine(("The union of " & zone1.ToString() & " and " & _
                        zone2.ToString() & " is null."))
                    Else
                        Console.WriteLine(("The union of " & zoneIdPerm1.SecurityZone.ToString() & " and " _
                        & zoneIdPerm2.SecurityZone.ToString() & " = " _
                        & CType(zoneIdPerm3, Object).SecurityZone.ToString()))
                    End If
ContinueWhile2:
                End While
ContinueWhile1:
            End While


            Return returnValue
        End Function 'UnionDemo

        ' Intersect creates and returns a new permission that is the intersection of the current 
        ' permission and the permission specified.
        Private Function IntersectDemo() As Boolean
            Dim returnValue As Boolean = True
            Dim zone1, zone2 As SecurityZone
            Dim zoneIdPerm1, zoneIdPerm2, zoneIdPerm3 As ZoneIdentityPermission
            Dim zoneGen1 As New zoneGenerator()
            Dim zoneGen2 As New zoneGenerator()
            zoneGen1.ResetIndex()
            While zoneGen1.CreateZone(zoneIdPerm1, zone1)
                If zoneIdPerm1 Is Nothing Then
                    GoTo ContinueWhile1
                End If
                Console.WriteLine("**********************************************************" & ControlChars.Cr)
                zoneGen2.ResetIndex()

                While zoneGen2.CreateZone(zoneIdPerm2, zone2)
                    If zoneIdPerm2 Is Nothing Then
                        GoTo ContinueWhile2
                    End If
                    zoneIdPerm3 = CType(zoneIdPerm1.Intersect(zoneIdPerm2), ZoneIdentityPermission)
                    If Not (zoneIdPerm3 Is Nothing) Then
                        Console.WriteLine(("The intersection of " & zone1.ToString() & " and " & _
                        zone2.ToString() & " = " & CType(zoneIdPerm3, Object).SecurityZone.ToString()))
                    Else
                        Console.WriteLine(("The intersection of " & zone1.ToString() & " and " & _
                        zone2.ToString() & " is null."))
                    End If
ContinueWhile2:
                End While
ContinueWhile1:
            End While

            Return returnValue
        End Function 'IntersectDemo

        'Copy creates and returns an identical copy of the current permission.
        Private Function CopyDemo() As Boolean

            Dim returnValue As Boolean = True

            Dim zone1 As SecurityZone
            Dim zoneIdPerm1, zoneIdPerm2 As ZoneIdentityPermission

            Dim zoneGen1 As New zoneGenerator()
            Dim zoneGen2 As New zoneGenerator()

            zoneGen1.ResetIndex()
            While zoneGen1.CreateZone(zoneIdPerm1, zone1)
                If zoneIdPerm1 Is Nothing Then
                    GoTo ContinueWhile1
                End If
                zoneGen2.ResetIndex()
                Console.WriteLine("********************************************************" & ControlChars.Cr)
                Try
                    zoneIdPerm2 = CType(zoneIdPerm1.Copy(), ZoneIdentityPermission)
                    Console.WriteLine(("Result of copy = " & zoneIdPerm2.ToString()))

                Catch e As Exception
                    Console.WriteLine(("Copy failed :" & zoneIdPerm1.ToString() & e.ToString()))
                    GoTo ContinueWhile1
                End Try
ContinueWhile1:
            End While
            Return returnValue
        End Function 'CopyDemo

        ' ToXml creates an XML encoding of the permission and its current state; 
        ' FromXml reconstructs a permission with the specified state from the XML encoding. 
        Private Function ToFromXmlDemo() As Boolean

            Dim returnValue As Boolean = True

            Dim zone1 As SecurityZone
            Dim zoneIdPerm1, zoneIdPerm2 As ZoneIdentityPermission
            Dim zoneGen1 As New zoneGenerator()
            Dim zoneGen2 As New zoneGenerator()
            zoneGen1.ResetIndex()
            While zoneGen1.CreateZone(zoneIdPerm1, zone1)
                If zoneIdPerm1 Is Nothing Then
                    GoTo ContinueWhile1
                End If
                Console.WriteLine("********************************************************" & ControlChars.Cr)
                zoneGen2.ResetIndex()
                Try
                    zoneIdPerm2 = New ZoneIdentityPermission(PermissionState.None)
                    zoneIdPerm2.FromXml(zoneIdPerm1.ToXml())
                    Console.WriteLine(("Result of ToFromXml = " & zoneIdPerm2.ToString()))

                Catch e As Exception
                    Console.WriteLine(("ToFromXml failed :" & zoneIdPerm1.ToString() & e.ToString()))
                    GoTo ContinueWhile1
                End Try
ContinueWhile1:
            End While

            Return returnValue
        End Function 'ToFromXmlDemo

        ' Invoke all demos.
        Public Function RunDemo() As Boolean
            Dim returnCode As Boolean = True
            Dim tempReturnCode As Boolean

            ' Call the IsSubsetOf demo.
            If IsSubsetOfDemo() Then
                Console.Out.WriteLine("IsSubsetOf demo completed successfully.")
                tempReturnCode = True
            Else
                Console.Out.WriteLine("IsSubsetOf demo failed.")
                tempReturnCode = False
            End If
            returnCode = tempReturnCode And returnCode

            ' Call the Union demo.
            If UnionDemo() Then
                Console.Out.WriteLine("Union demo completed successfully.")
                tempReturnCode = True
            Else
                Console.Out.WriteLine("Union demo failed.")
                tempReturnCode = False
            End If
            returnCode = tempReturnCode And returnCode

            ' Call the Intersect demo.    
            If IntersectDemo() Then
                Console.Out.WriteLine("Intersect demo completed successfully.")
                tempReturnCode = True
            Else
                Console.Out.WriteLine("Intersect demo failed.")
                tempReturnCode = False
            End If
            returnCode = tempReturnCode And returnCode


            ' Call the Copy demo.    
            If CopyDemo() Then
                Console.Out.WriteLine("Copy demo completed successfully.")
                tempReturnCode = True
            Else
                Console.Out.WriteLine("Copy demo failed.")
                tempReturnCode = False
            End If
            returnCode = tempReturnCode And returnCode

            ' Call the ToFromXml demo.    
            If ToFromXmlDemo() Then
                Console.Out.WriteLine("ToFromXml demo completed successfully.")
                tempReturnCode = True
            Else
                Console.Out.WriteLine("ToFromXml demo failed.")
                tempReturnCode = False
            End If
            returnCode = tempReturnCode And returnCode

            Return returnCode
        End Function

        ' Test harness.
        Public Overloads Shared Sub Main(ByVal args() As [String])
            Try
                Dim democase As New ZoneIdentityDemo()
                Dim returnCode As Boolean = democase.RunDemo()
                If returnCode Then
                    Console.Out.WriteLine("ZoneIdentityPermission demo completed successfully.")
                    Console.Out.WriteLine("Press the Enter key to exit.")
                    Dim consoleInput As String = Console.ReadLine()
                    System.Environment.ExitCode = 100
                Else
                    Console.Out.WriteLine("ZoneIdentityPermission demo failed.")
                    Console.Out.WriteLine("Press the Enter key to exit.")
                    Dim consoleInput As String = Console.ReadLine()
                    System.Environment.ExitCode = 101
                End If
            Catch e As Exception
                Console.Out.WriteLine("ZoneIdentityPermission demo failed.")
                Console.WriteLine(e.ToString())
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 101
            End Try
        End Sub 'Main
    End Class 'ZoneIdentityDemo
 _


    ' This class generates ZoneIdentityPermission objects.
    Friend Class ZoneGenerator


        Private myZone As SecurityZone() = {SecurityZone.NoZone, _
        SecurityZone.Internet, _
        SecurityZone.Intranet, _
        SecurityZone.MyComputer, _
        SecurityZone.Untrusted, _
        SecurityZone.Trusted}

        Private zoneIndex As Integer = 0


        Public Sub New()
            ResetIndex()
        End Sub 'New


        Public Sub ResetIndex()
            zoneIndex = 0
        End Sub 'ResetIndex

        ' CreateZone creates ZoneIdentityPermission objects.
        Public Function CreateZone(ByRef zonePerm As ZoneIdentityPermission, ByRef zone As SecurityZone) As Boolean

            If zoneIndex >= myZone.Length Then
                zonePerm = New ZoneIdentityPermission(PermissionState.None)
                zone = SecurityZone.NoZone
                zoneIndex = zoneIndex + 1
                Return False
            End If
            zone = myZone(zoneIndex)
            zoneIndex = zoneIndex + 1
            Try
                zonePerm = New ZoneIdentityPermission(zone)
                Return True
            Catch e As Exception
                Console.WriteLine(("Cannot create ZoneIdentityPermission: " & zone & " " & e.ToString()))
                zonePerm = New ZoneIdentityPermission(PermissionState.None)
                zone = SecurityZone.NoZone
                Return True
            End Try
        End Function 'CreateZone
    ' End of ZoneGenerator.
End Module

[C#] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
// of the ZoneIdentityPermission class.

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

[assembly:CLSCompliant(true)]

public class ZoneIdentityDemo
{
// IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private bool IsSubsetOfDemo()
    {
        bool returnValue = true;
        SecurityZone zone1,zone2;
        ZoneIdentityPermission zoneIdPerm1,zoneIdPerm2;
        ZoneGenerator zoneGen1 = new ZoneGenerator();
        ZoneGenerator zoneGen2 = new ZoneGenerator();
        zoneGen1.ResetIndex();
        while(zoneGen1.CreateZone(out zoneIdPerm1, out zone1)) 
        {
            if(zoneIdPerm1 == null) continue;
            zoneGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");

            while(zoneGen2.CreateZone(out zoneIdPerm2, out zone2)) 
            {
                if(zoneIdPerm2 == null) continue;
                try
                {            
                    if (zoneIdPerm1.IsSubsetOf(zoneIdPerm2))
                    {
                        Console.WriteLine(zoneIdPerm1.SecurityZone + " is a subset of " + 
                            zoneIdPerm2.SecurityZone);
                    }
                    else
                    {
                        Console.WriteLine(zoneIdPerm1.SecurityZone + " is not a subset of " + 
                            zoneIdPerm2.SecurityZone);
                    }
                }
                catch(Exception e) 
                {
                    Console.WriteLine("An exception was thrown for subset :" + zoneIdPerm1 + "\n" + 
                        zoneIdPerm2 +"\n" + e);
                    returnValue=false;
                }
            }
        }
        return returnValue;
    }
// Union creates a new permission that is the union of the current permission and the specified permission.
    private bool UnionDemo()
    {
        bool returnValue = true;
        SecurityZone zone1,zone2;
        ZoneIdentityPermission zoneIdPerm1,zoneIdPerm2;
        IPermission zoneIdPerm3;
        ZoneGenerator zoneGen1 = new ZoneGenerator();
        ZoneGenerator zoneGen2 = new ZoneGenerator();
        zoneGen1.ResetIndex();
        while(zoneGen1.CreateZone(out zoneIdPerm1, out zone1)) 
        {
            if(zoneIdPerm1 == null) continue;
            Console.WriteLine("**********************************************************\n");
            zoneGen2.ResetIndex();

            while(zoneGen2.CreateZone(out zoneIdPerm2, out zone2)) 
            {
                if(zoneIdPerm2 == null) continue;
                zoneIdPerm3 = (ZoneIdentityPermission)zoneIdPerm1.Union(zoneIdPerm2);
                zoneIdPerm3 = zoneIdPerm1.Union(zoneIdPerm2);

                if(zoneIdPerm3 == null)  
                {
                    Console.WriteLine("The union of " +  zone1 + " and " + zone2 + " is null." );
                }
                else
                {
                    Console.WriteLine("The union of " + zoneIdPerm1.SecurityZone + " and " + 
                        zoneIdPerm2.SecurityZone + " = " + 
                        ((ZoneIdentityPermission)zoneIdPerm3).SecurityZone.ToString());
                }

            }
        }

        return returnValue;

    }
// Intersect creates and returns a new permission that is the intersection of the current 
// permission and the permission specified.
    private bool IntersectDemo()
    {
        bool returnValue = true;
        SecurityZone zone1,zone2;
        ZoneIdentityPermission zoneIdPerm1,zoneIdPerm2,zoneIdPerm3;
        ZoneGenerator zoneGen1 = new ZoneGenerator();
        ZoneGenerator zoneGen2 = new ZoneGenerator();
        zoneGen1.ResetIndex();
        while(zoneGen1.CreateZone(out zoneIdPerm1, out zone1)) 
        {
            if(zoneIdPerm1 == null) continue;
            Console.WriteLine("**********************************************************\n");
            zoneGen2.ResetIndex();

            while(zoneGen2.CreateZone(out zoneIdPerm2, out zone2)) 
            {
                if(zoneIdPerm2 == null) continue;
    
                zoneIdPerm3 = (ZoneIdentityPermission)zoneIdPerm1.Intersect(zoneIdPerm2);
                if (zoneIdPerm3 != null)
                {
                    Console.WriteLine("The intersection of " + zone1 + " and " + zone2 + " = " + 
                        ((ZoneIdentityPermission)zoneIdPerm3).SecurityZone.ToString());
                }
                else
                {
                    Console.WriteLine("The intersection of " + zone1 + " and " + zone2 + " is null." );
                }
            }
        }

        return returnValue;
    }
//Copy creates and returns an identical copy of the current permission.
    private bool CopyDemo()
    {

        bool returnValue = true;

        SecurityZone zone1;
        ZoneIdentityPermission zoneIdPerm1,zoneIdPerm2;

        ZoneGenerator zoneGen1 = new ZoneGenerator();
        ZoneGenerator zoneGen2 = new ZoneGenerator();

        zoneGen1.ResetIndex();
        while(zoneGen1.CreateZone(out zoneIdPerm1, out zone1)) 
        {
            if(zoneIdPerm1 == null) continue;
            zoneGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");
            try
            {            
                zoneIdPerm2 = (ZoneIdentityPermission)zoneIdPerm1.Copy();
                Console.WriteLine("Result of copy = " + zoneIdPerm2.ToString());

            }
            catch(Exception e) 
            {
                Console.WriteLine("Copy failed :" + zoneIdPerm1.ToString() + e);
                continue;
            }
        }
        return returnValue;
    }
// ToXml creates an XML encoding of the permission and its current state; 
// FromXml reconstructs a permission with the specified state from the XML encoding. 
    private bool ToFromXmlDemo()
    {

        bool returnValue = true;

        SecurityZone zone1;
        ZoneIdentityPermission zoneIdPerm1,zoneIdPerm2;
        ZoneGenerator zoneGen1 = new ZoneGenerator();
        ZoneGenerator zoneGen2 = new ZoneGenerator();
        zoneGen1.ResetIndex();
        while(zoneGen1.CreateZone(out zoneIdPerm1, out zone1)) 
        {
            if(zoneIdPerm1 == null) continue;
            Console.WriteLine("********************************************************\n");
            zoneGen2.ResetIndex();
        try
            {            
                zoneIdPerm2= new ZoneIdentityPermission(PermissionState.None);
                zoneIdPerm2.FromXml(zoneIdPerm1.ToXml());
                Console.WriteLine("Result of ToFromXml = " +
                    zoneIdPerm2.ToString());
    
            }
            catch(Exception e) 
            {
                Console.WriteLine("ToFromXml failed :" + zoneIdPerm1.ToString() + e);
                continue;
            }
        }

        return returnValue;

    }
// Invoke all demos.
    public bool RunDemo()
    {
        bool returnCode = true;
        bool tempReturnCode;

        // Call the IsSubsetOf demo.
        if(tempReturnCode= IsSubsetOfDemo())Console.Out.WriteLine("IsSubsetOf demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOf demo failed.");
        returnCode=tempReturnCode && returnCode;

        // Call the Union demo.
        if(tempReturnCode= UnionDemo())Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        returnCode=tempReturnCode && returnCode;

        // Call the Intersect demo.    
        if(tempReturnCode= IntersectDemo())Console.Out.WriteLine("Intersect demo completed successfully.");
        else Console.Out.WriteLine("Intersect demo failed.");
        returnCode=tempReturnCode && returnCode;


        // Call the Copy demo.    
        if(tempReturnCode= CopyDemo())Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        returnCode=tempReturnCode && returnCode;

        // Call the ToFromXml demo.    
        if(tempReturnCode= ToFromXmlDemo())Console.Out.WriteLine("ToFromXml demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        returnCode=tempReturnCode && returnCode;

        return (  returnCode );    
    }
// Test harness.
    public static void Main(String[] args) 
    {
        try
        {
            ZoneIdentityDemo democase = new ZoneIdentityDemo();
            bool returnCode = democase.RunDemo();
            if (returnCode)
            {
                Console.Out.WriteLine("ZoneIdentityPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("ZoneIdentityPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch(Exception e)
        {
            Console.Out.WriteLine("ZoneIdentityPermission demo failed.");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}


// This class generates ZoneIdentityPermission objects.

internal  class ZoneGenerator
{


    private SecurityZone[] myZone = 
{SecurityZone.NoZone,SecurityZone.Internet,SecurityZone.Intranet,SecurityZone.MyComputer,SecurityZone.Untrusted,SecurityZone.Trusted};

    private int zoneIndex = 0;

    public ZoneGenerator()
    {
        ResetIndex();
    }

    public void ResetIndex()
    {
        zoneIndex = 0;
    }
// CreateZone creates ZoneIdentityPermission objects.
    public bool CreateZone(out ZoneIdentityPermission zonePerm, out SecurityZone zone)
    {

        if(zoneIndex >= myZone.Length) 
        {
            zonePerm = new ZoneIdentityPermission(PermissionState.None);
            zone=SecurityZone.NoZone;
            zoneIndex++;
            return false;
        }
        zone = myZone[zoneIndex++];
        try
        {
            zonePerm = new ZoneIdentityPermission(zone);
            return true;
        } 
        catch(Exception e)
        {
            Console.WriteLine("Cannot create ZoneIdentityPermission: " + zone +" "+e);
            zonePerm = new ZoneIdentityPermission(PermissionState.None);
            zone=SecurityZone.NoZone;
            return true;
        }
    }
} // End of ZoneGenerator.

[C++] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods
// of the ZoneIdentityPermission class.

#using <mscorlib.dll>

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Runtime::InteropServices;

[assembly:CLSCompliant(true)];

// This class generates ZoneIdentityPermission objects.
__gc class ZoneGenerator {
private:
   SecurityZone myZone[];
   int  zoneIndex;

public:
   ZoneGenerator() {
      SecurityZone temp[] = {SecurityZone::NoZone, SecurityZone::Internet, SecurityZone::Intranet, SecurityZone::MyComputer, SecurityZone::Untrusted, SecurityZone::Trusted};
      myZone = temp;
      ResetIndex();
   };

   void ResetIndex() {
      zoneIndex = 0;
   };

   // CreateZone creates ZoneIdentityPermission objects.
public:
   bool CreateZone([Out] ZoneIdentityPermission** zonePerm, [Out] SecurityZone* zone) {

      if (zoneIndex >= myZone->Length) {
         *zonePerm = new ZoneIdentityPermission(PermissionState::None);
         *zone=SecurityZone::NoZone;
         zoneIndex++;
         return false;
      };
      *zone = myZone[zoneIndex++];
      try {
         *zonePerm = new ZoneIdentityPermission(*zone);
         return true;
      } catch (Exception* e) {
         Console::WriteLine(S"Cannot create ZoneIdentityPermission: {0} {1}", __box(*zone), e);
         *zonePerm = new ZoneIdentityPermission(PermissionState::None);
         *zone=SecurityZone::NoZone;
         return false;
      };
   };
}; // End of ZoneGenerator.

public __gc class ZoneIdentityDemo {
   // IsSubsetOf determines whether the current permission is a subset of the specified permission.
private:
   bool IsSubsetOfDemo() {
      bool returnValue = true;
      SecurityZone zone1, zone2;
      ZoneIdentityPermission* zoneIdPerm1, * zoneIdPerm2;
      ZoneGenerator* zoneGen1 = new ZoneGenerator();
      ZoneGenerator* zoneGen2 = new ZoneGenerator();
      zoneGen1->ResetIndex();
      while(zoneGen1->CreateZone(&zoneIdPerm1, &zone1)) {
         if (zoneIdPerm1 == 0) continue;
         zoneGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");

         while(zoneGen2->CreateZone(&zoneIdPerm2, &zone2)) {
            if (zoneIdPerm2 == 0) continue;
            try {
               if (zoneIdPerm1->IsSubsetOf(zoneIdPerm2)) {
                  Console::WriteLine(S"{0} is a subset of {1}",
                     __box(zoneIdPerm1->SecurityZone), __box(zoneIdPerm2->SecurityZone));
               } else {
                  Console::WriteLine(S"{0} is not a subset of {1}",
                     __box(zoneIdPerm1->SecurityZone), __box(zoneIdPerm2->SecurityZone));
               }
            } catch (Exception* e) {
               Console::WriteLine(S"An exception was thrown for subset : {0}\n{1}\n{2}",
                  zoneIdPerm1, zoneIdPerm2, e);
               returnValue=false;
            }
         }
      }
      return returnValue;
   }

   // Union creates a new permission that is the union of the current permission and the specified permission.
private:
   bool UnionDemo() {
      bool returnValue = true;
      SecurityZone zone1, zone2;
      ZoneIdentityPermission* zoneIdPerm1, *zoneIdPerm2;
      IPermission* zoneIdPerm3;
      ZoneGenerator* zoneGen1 = new ZoneGenerator();
      ZoneGenerator* zoneGen2 = new ZoneGenerator();
      zoneGen1->ResetIndex();
      while(zoneGen1->CreateZone(&zoneIdPerm1, &zone1)) {
         if (zoneIdPerm1 == 0) continue;
         Console::WriteLine(S"**********************************************************\n");
         zoneGen2->ResetIndex();

         while(zoneGen2->CreateZone(&zoneIdPerm2, &zone2)) {
            if (zoneIdPerm2 == 0) continue;
            zoneIdPerm3 = zoneIdPerm1->Union(zoneIdPerm2);

            if (zoneIdPerm3 == 0) {
               Console::WriteLine(S"The union of {0} and {1} is null.",
                  __box(zone1), __box(zone2));
            } else {
               Console::WriteLine(S"The union of {0} and {1} = {2}",
                  __box(zoneIdPerm1->SecurityZone), __box(zoneIdPerm2->SecurityZone),
                  __box((dynamic_cast<ZoneIdentityPermission*>(zoneIdPerm3))->SecurityZone));
            }
         }
      }
      return returnValue;
   }

   // Intersect creates and returns a new permission that is the intersection of the current
   // permission and the permission specified.
private:
   bool IntersectDemo() {
      bool returnValue = true;
      SecurityZone zone1, zone2;
      ZoneIdentityPermission* zoneIdPerm1, *zoneIdPerm2, *zoneIdPerm3;
      ZoneGenerator* zoneGen1 = new ZoneGenerator();
      ZoneGenerator* zoneGen2 = new ZoneGenerator();
      zoneGen1->ResetIndex();
      while(zoneGen1->CreateZone(&zoneIdPerm1, &zone1)) {
         if (zoneIdPerm1 == 0) continue;
         Console::WriteLine(S"**********************************************************\n");
         zoneGen2->ResetIndex();

         while(zoneGen2->CreateZone(&zoneIdPerm2, &zone2)) {
            if (zoneIdPerm2 == 0) continue;

            zoneIdPerm3 = dynamic_cast<ZoneIdentityPermission*>(zoneIdPerm1->Intersect(zoneIdPerm2));
            if (zoneIdPerm3 != 0) {
               Console::WriteLine(S"The intersection of {0} and {1} = {2}", __box(zone1),
                  __box(zone2),
                  __box((dynamic_cast<ZoneIdentityPermission*>(zoneIdPerm3))->SecurityZone));
            } else {
               Console::WriteLine(S"The intersection of {0} and {1} is null.", __box(zone1), __box(zone2));
            }
         }
      }
      return returnValue;
   }
   //Copy creates and returns an identical copy of the current permission.
private:
   bool CopyDemo() {

      bool returnValue = true;

      SecurityZone zone1;
      ZoneIdentityPermission* zoneIdPerm1, *zoneIdPerm2;

      ZoneGenerator* zoneGen1 = new ZoneGenerator();
      ZoneGenerator* zoneGen2 = new ZoneGenerator();

      zoneGen1->ResetIndex();
      while(zoneGen1->CreateZone(&zoneIdPerm1, &zone1)) {
         if (zoneIdPerm1 == 0) continue;
         zoneGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");
         try {
            zoneIdPerm2 = dynamic_cast<ZoneIdentityPermission*>(zoneIdPerm1->Copy());
            Console::WriteLine(S"Result of copy = {0}", zoneIdPerm2);
         } catch (Exception* e) {
            Console::WriteLine(S"Copy failed : {0}{1}", zoneIdPerm1, e);
            continue;
         }
      }
      return returnValue;
   }
   // ToXml creates an XML encoding of the permission and its current state;
   // FromXml reconstructs a permission with the specified state from the XML encoding.
private:
   bool ToFromXmlDemo() {

      bool returnValue = true;

      SecurityZone zone1;
      ZoneIdentityPermission* zoneIdPerm1, *zoneIdPerm2;
      ZoneGenerator* zoneGen1 = new ZoneGenerator();
      ZoneGenerator* zoneGen2 = new ZoneGenerator();
      zoneGen1->ResetIndex();
      while(zoneGen1->CreateZone(&zoneIdPerm1, &zone1)) {
         if (zoneIdPerm1 == 0) continue;
         Console::WriteLine(S"********************************************************\n");
         zoneGen2->ResetIndex();
         try {
            zoneIdPerm2 = new ZoneIdentityPermission(PermissionState::None);
            zoneIdPerm2->FromXml(zoneIdPerm1->ToXml());
            Console::WriteLine(S"Result of ToFromXml = {0}", zoneIdPerm2);
         } catch (Exception* e) {
            Console::WriteLine(S"ToFromXml failed : {0}{1}", zoneIdPerm1, e);
            continue;
         }
      }

      return returnValue;

   }
   // Invoke all demos.
public:
   bool RunDemo() {
      bool returnCode = true;
      bool tempReturnCode;

      // Call the IsSubsetOf demo.
      if (tempReturnCode= IsSubsetOfDemo())
         Console::WriteLine(S"IsSubsetOf demo completed successfully.");
      else
         Console::WriteLine(S"IsSubsetOf demo failed.");
      returnCode=tempReturnCode && returnCode;
      // Call the Union demo.
      if (tempReturnCode= UnionDemo())
         Console::WriteLine(S"Union demo completed successfully.");
      else
         Console::WriteLine(S"Union demo failed.");
      returnCode=tempReturnCode && returnCode;

      // Call the Intersect demo.
      if (tempReturnCode= IntersectDemo())
         Console::WriteLine(S"Intersect demo completed successfully.");
      else
         Console::WriteLine(S"Intersect demo failed.");
      returnCode=tempReturnCode && returnCode;

      // Call the Copy demo.
      if (tempReturnCode= CopyDemo())
         Console::WriteLine(S"Copy demo completed successfully.");
      else
         Console::WriteLine(S"Copy demo failed.");
      returnCode=tempReturnCode && returnCode;

      // Call the ToFromXml demo.
      if (tempReturnCode= ToFromXmlDemo())
         Console::WriteLine(S"ToFromXml demo completed successfully.");
      else
         Console::WriteLine(S"ToFromXml demo failed.");
      returnCode=tempReturnCode && returnCode;

      return (returnCode);
   }
};

// Test harness.
int main() {
   try {
      ZoneIdentityDemo* democase = new ZoneIdentityDemo();
      bool returnCode = democase->RunDemo();
      if (returnCode) {
         Console::WriteLine(S"ZoneIdentityPermission demo completed successfully.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 100;
      } else {
         Console::WriteLine(S"ZoneIdentityPermission demo failed.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 101;
      }
   } catch (Exception* e) {
      Console::WriteLine(S"ZoneIdentityPermission demo failed.");
      Console::WriteLine(e);
      Console::WriteLine(S"Press the Enter key to exit.");
      Console::ReadLine();
      System::Environment::ExitCode = 101;
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Security.Permissions

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

ZoneIdentityPermission メンバ | System.Security.Permissions 名前空間 | アクセス許可 | アクセス許可の要求 | ZoneIdentityPermissionAttribute | Zone | ZoneMembershipCondition