Share via


UrlIdentityPermission クラス

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

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

System.Object
   System.Security.CodeAccessPermission
      System.Security.Permissions.UrlIdentityPermission

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

スレッドセーフ

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

解説

プロトコル (HTTP、HTTPS、FTP) やファイル名を含む完全な URL が想定されます。たとえば、https://www.fourthcoffee.com/process/grind.htm は完全な URL です。

URL には、完全に一致する文字列、または最終位置にワイルドカードを使用した文字列 (https://www.fourthcoffee.com/process/\* など) を使用できます。

注意    UrlIdentityPermission は、URL と IP アドレスを含め、ファイルへのすべてのパスに対するアクセス許可を与えます。ファイルへのアクセスを Deny によって拒否するには、そのファイルへの有効なパスをすべて Deny によって拒否する必要があります。たとえば、www.forthcoffee.com の IP アドレスが 192.168.238.241 の場合、http://www.forthcoffee.com/process/grind.htm へのアクセスを Deny によって拒否するには、http://www.forthcoffee.com/process/grind.htm、192.168.238.241/grind.htm を含め、そのコードにアクセスために使用できるすべてのパスを Deny によって拒否する必要があります。ただし、URL のパスはきわめて多くの方法によって表記できるため、 Deny だけを使用してすべてのパスをブロックすることはきわめて困難です。複数パスの問題に対処するには、 PermitOnlyDeny を組み合わせることをお勧めします。 PermitOnly を使用すると、アクセスを許可する特定の URL のセットを指定でき、その上で Deny を使用すると、この中からアクセスを拒否するアドレスを明示的に指定できます。この詳細および DenyPermitOnly を使用する場合の詳細については、「 Deny 」の Canonicalization Problems Using Deny を参照してください。

使用例

 
' This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
' of the UrlIdentityPermission class.

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports Microsoft.VisualBasic

<Assembly: CLSCompliant(True)> 

Public Class UrlIdentityPermissionDemo

    ' IsSubsetOf determines whether the current permission is a subset of the specified permission.
    Private Function IsSubsetOfDemo() As Boolean

        Dim returnValue As Boolean = True

        Dim url1, url2 As String
        Dim urlIdPerm1, urlIdPerm2 As UrlIdentityPermission

        Dim urlGen1 As New UrlGenerator()
        Dim urlGen2 As New UrlGenerator()

        urlGen1.ResetIndex()
        While urlGen1.CreateUrl(urlIdPerm1, url1)
            If urlIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            urlGen2.ResetIndex()
            Console.WriteLine("********************************************************\n")
            While urlGen2.CreateUrl(urlIdPerm2, url2)
                Dim firstPermission As String = IIf(url1 = "" Or url1 Is Nothing, "null", url1)
                Dim secondPermission As String = IIf(url2 = "" Or url2 Is Nothing, "null", url2)
                If urlIdPerm2 Is Nothing Then
                    GoTo ContinueWhile2
                End If
                Try
                    If urlIdPerm1.IsSubsetOf(urlIdPerm2) Then

                        Console.WriteLine(firstPermission & ControlChars.Lf & " is a subset of " & secondPermission & ControlChars.Lf)
                    Else
                        Console.WriteLine(firstPermission & ControlChars.Lf & " is not a subset of " & secondPermission & ControlChars.Lf)
                    End If

                Catch e As Exception
                    Console.WriteLine(IIf("An exception was thrown for subset :" & url1 = "", "null.", IIf(url1 & ControlChars.Lf & url2 = "", "null.", url2 & ControlChars.Lf & e.ToString())))
                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 url1, url2 As String
        Dim urlIdPerm1, urlIdPerm2 As UrlIdentityPermission
        Dim urlIdPerm3 As IPermission

        Dim urlGen1 As New UrlGenerator()
        Dim urlGen2 As New UrlGenerator()

        urlGen1.ResetIndex()
        While urlGen1.CreateUrl(urlIdPerm1, url1)
            If urlIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            urlGen2.ResetIndex()
            While urlGen2.CreateUrl(urlIdPerm2, url2)

                Try
                    If urlIdPerm2 Is Nothing Then
                        GoTo ContinueWhile2
                    End If
                    Dim firstPermission As String = IIf(url1 = "" Or url1 Is Nothing, "null", url1)
                    Dim secondPermission As String = IIf(url2 = "" Or url2 Is Nothing, "null", url2)
                    urlIdPerm3 = CType(urlIdPerm1.Union(urlIdPerm2), UrlIdentityPermission)
                    urlIdPerm3 = urlIdPerm1.Union(urlIdPerm2)

                    If urlIdPerm3 Is Nothing Then
                        Console.WriteLine("The union of " & firstPermission & "  and " & ControlChars.Lf & ControlChars.Tab & secondPermission & " is null.")
                    Else
                        Console.WriteLine("The union of " & firstPermission & "  and " & ControlChars.Lf & ControlChars.Tab & secondPermission & " = " & ControlChars.Lf & ControlChars.Tab & CType(urlIdPerm3, UrlIdentityPermission).Url.ToString())
                    End If
                Catch e As Exception
                    Console.WriteLine("An exception was thrown for union :" & e.ToString())
                    returnValue = False
                End Try
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 url1, url2 As String
        Dim urlIdPerm1, urlIdPerm2, urlIdPerm3 As UrlIdentityPermission

        Dim urlGen1 As New UrlGenerator()
        Dim urlGen2 As New UrlGenerator()

        urlGen1.ResetIndex()
        While urlGen1.CreateUrl(urlIdPerm1, url1)
            If urlIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            urlGen2.ResetIndex()
            Console.WriteLine("********************************************************\n")
            While urlGen2.CreateUrl(urlIdPerm2, url2)
                If urlIdPerm2 Is Nothing Then
                    GoTo ContinueWhile2
                End If
                Dim firstPermission As String = IIf(url1 = "" Or url1 Is Nothing, "null", url1)
                Dim secondPermission As String = IIf(url2 = "" Or url2 Is Nothing, "null", url2)
                Try

                    urlIdPerm3 = CType(urlIdPerm1.Intersect(urlIdPerm2), UrlIdentityPermission)
                    If Not (urlIdPerm3 Is Nothing) AndAlso Not (urlIdPerm3.Url Is Nothing) Then

                        Console.WriteLine("The intersection of " & firstPermission & "  and " & ControlChars.Lf & ControlChars.Tab & secondPermission & " = " & ControlChars.Lf & ControlChars.Tab & CType(urlIdPerm3, UrlIdentityPermission).Url.ToString())
                    Else
                        Console.WriteLine("The intersection of " & firstPermission & "  and " & ControlChars.Lf & ControlChars.Tab & secondPermission & " is null. ")
                    End If
                Catch e As Exception
                    Console.WriteLine("An exception was thrown for the intersection : " & e.ToString())
                    returnValue = False
                End Try
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 url1 As String
        Dim urlIdPerm1, urlIdPerm2 As UrlIdentityPermission
        Dim urlGen1 As New UrlGenerator()
        Dim urlGen2 As New UrlGenerator()

        urlGen1.ResetIndex()
        While urlGen1.CreateUrl(urlIdPerm1, url1)
            If urlIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            urlGen2.ResetIndex()
            Console.WriteLine("********************************************************\n")
            Try
                urlIdPerm2 = CType(urlIdPerm1.Copy(), UrlIdentityPermission)
                If Not (urlIdPerm2 Is Nothing) Then
                    Console.WriteLine("Result of copy = " & urlIdPerm2.ToString() & ControlChars.Lf)
                Else
                    Console.WriteLine("Result of copy is null. " & ControlChars.Lf)
                End If
            Catch e As Exception
                If (True.ToString()) Then
                    If url1 = "" Then
                        Console.WriteLine("The target UrlIdentityPermission is empty; copy failed.")

                    Else
                        Console.WriteLine(e.ToString())
                    End If
                End If
                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 url1 As String
        Dim urlIdPerm1, urlIdPerm2 As UrlIdentityPermission

        Dim urlGen1 As New UrlGenerator()
        Dim urlGen2 As New UrlGenerator()

        urlGen1.ResetIndex()
        While urlGen1.CreateUrl(urlIdPerm1, url1)
            If urlIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            Console.WriteLine("********************************************************\n")
            urlGen2.ResetIndex()
            Try
                urlIdPerm2 = New UrlIdentityPermission(PermissionState.None)
                urlIdPerm2.FromXml(urlIdPerm1.ToXml())
                Console.WriteLine("Result of ToFromXml = " & urlIdPerm2.ToString())

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

        Return returnValue
    End Function 'ToFromXmlDemo

    ' Invoke all demos.
    Public Function runDemo() As Boolean

        Dim ret As Boolean = True
        Dim retTmp As Boolean
        ' Call IsSubsetOf demo.
        retTmp = IsSubsetOfDemo()
        If retTmp Then
            Console.Out.WriteLine("IsSubset demo completed successfully.")
        Else
            Console.Out.WriteLine("IsSubset demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call Union demo.
        retTmp = UnionDemo()
        If retTmp Then
            Console.Out.WriteLine("Union demo completed successfully.")
        Else
            Console.Out.WriteLine("Union demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call Intersect demo.    
        retTmp = IntersectDemo()
        If retTmp Then
            Console.Out.WriteLine("Intersect demo completed successfully.")
        Else
            Console.Out.WriteLine("Intersect demo failed.")
        End If
        ret = retTmp AndAlso ret


        ' Call Copy demo.    
        retTmp = CopyDemo()
        If retTmp Then
            Console.Out.WriteLine("Copy demo completed successfully.")
        Else
            Console.Out.WriteLine("Copy demo failed.")
        End If
        ret = retTmp AndAlso ret

        ' Call ToFromXml demo.
        retTmp = ToFromXmlDemo()
        If retTmp Then
            Console.Out.WriteLine("ToFromXml demo completed successfully.")
        Else
            Console.Out.WriteLine("ToFromXml demo failed.")
        End If
        ret = retTmp AndAlso ret

        Return ret
    End Function 'runDemo

    ' Test harness.
    Public Overloads Shared Sub Main(ByVal args() As [String])
        Try
            Dim democase As New UrlIdentityPermissionDemo()
            Dim ret As Boolean = democase.runDemo()
            If ret Then
                Console.Out.WriteLine("UrlIdentity 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("UrlIdentity 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("UrlIdentity 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 'UrlIdentityPermissionDemo


' This class generates UrlIdentityPermission objects.

Friend Class UrlGenerator


    Private myUrl As String() = {"https://www.fourthcoffee.com/process/grind.htm", "https://www.fourthcoffee.com/process/*", "https://www.fourthcoffee.com/*", "https://www.fourthcoffee.com/process/*", "https://www.contoso.com/math.asmx"}

    Private urlIndex As Integer = 0


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


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

    ' CreateUrl creates a UrlIdentityPermission.
    Public Function CreateUrl(ByRef urlPerm As UrlIdentityPermission, ByRef url As String) As Boolean


        If urlIndex = myUrl.Length Then
            urlPerm = New UrlIdentityPermission(PermissionState.None)
            url = ""
            urlIndex &= 1
            Return True
        End If
        If urlIndex > myUrl.Length Then
            urlPerm = Nothing
            url = ""
            Return False
        End If

        url = myUrl(urlIndex)
        urlIndex = urlIndex + 1

        Try
            urlPerm = New UrlIdentityPermission(url)
            Return True
        Catch e As Exception
            Console.WriteLine("Cannot create UrlIdentityPermission : " & url & " " & e.ToString())
            urlPerm = New UrlIdentityPermission(PermissionState.None)
            url = ""
            Return True
        End Try
    End Function 'CreateUrl
' End of UrlGenerator.

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


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

    [assembly:CLSCompliant(true)]

    public class UrlIdentityPermissionDemo
    {
// IsSubsetOf determines whether the current permission is a subset of the specified permission.
        private bool IsSubsetOfDemo()
        {

            bool returnValue = true;

            string url1,url2;
            UrlIdentityPermission urlIdPerm1,urlIdPerm2;

            UrlGenerator urlGen1 = new UrlGenerator();
            UrlGenerator urlGen2 = new UrlGenerator();

            urlGen1.ResetIndex();
            while(urlGen1.CreateUrl(out urlIdPerm1, out url1)) 
            {
                if(urlIdPerm1 == null) continue;
                urlGen2.ResetIndex();
                Console.WriteLine("********************************************************\n");
                while(urlGen2.CreateUrl(out urlIdPerm2, out url2)) 
                {
                    string firstPermission = url1 == "" | url1 == null ? "null" : url1 ;
                    string secondPermission = url2 == "" | url2 == null ? "null" : url2;
                    if(urlIdPerm2 == null) continue;
                    try
                    {            
                        if (urlIdPerm1.IsSubsetOf(urlIdPerm2))
                        {
                            
                            Console.WriteLine(firstPermission +"\n is a subset of " 
                                + secondPermission + "\n");
                        }
                        else
                        {
                            Console.WriteLine(firstPermission  +"\n is not a subset of " 
                                + secondPermission + "\n");
                        }

                    }
                    catch(Exception e) 
                    {
                        Console.WriteLine("An exception was thrown for subset :" 
                            + url1 == "" ? "null." : url1 + "\n" + url2 == "" ? "null." : url2 +"\n" + e);
                    }
                }
            }
            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;

            string url1,url2;
            UrlIdentityPermission urlIdPerm1,urlIdPerm2;
            IPermission urlIdPerm3;

            UrlGenerator urlGen1 = new UrlGenerator();
            UrlGenerator urlGen2 = new UrlGenerator();

            urlGen1.ResetIndex();
            while(urlGen1.CreateUrl(out urlIdPerm1, out url1)) 
            {
                if(urlIdPerm1 == null) continue;
                
                Console.WriteLine("**********************************************************\n");
                urlGen2.ResetIndex();
                    while(urlGen2.CreateUrl(out urlIdPerm2, out url2)) 
                    {

                        try
                        {
                            if(urlIdPerm2 == null) continue;

                            string firstPermission = url1 == "" | url1 == null ? "null" : url1 ;
                            string secondPermission = url2 == "" | url2 == null ? "null" : url2;
                            urlIdPerm3 = (UrlIdentityPermission)urlIdPerm1.Union(urlIdPerm2);
                            urlIdPerm3 = urlIdPerm1.Union(urlIdPerm2);

                            if(urlIdPerm3 == null) 
                            {
                                Console.WriteLine("The union of " + firstPermission 
                                    + "  and \n\t"  + secondPermission + " is null.");
                            }
                            else
                            {
                                Console.WriteLine("The union of " + firstPermission + "  and \n\t"  
                                    + secondPermission + 
                                    " = \n\t" + ((UrlIdentityPermission)urlIdPerm3).Url.ToString());
                            }
                        }
                        catch(Exception e) 
                        {
                            Console.WriteLine("An exception was thrown for union :" + e);
                            returnValue=false;
                        }

                }
                
            }

            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;

            string url1,url2;
            UrlIdentityPermission urlIdPerm1,urlIdPerm2,urlIdPerm3;

            UrlGenerator urlGen1 = new UrlGenerator();
            UrlGenerator urlGen2 = new UrlGenerator();

            urlGen1.ResetIndex();
            while(urlGen1.CreateUrl(out urlIdPerm1, out url1)) 
            {
                if(urlIdPerm1 == null) continue;
                Console.WriteLine("**********************************************************\n");
                urlGen2.ResetIndex();
                Console.WriteLine("********************************************************\n");
                while(urlGen2.CreateUrl(out urlIdPerm2, out url2)) 
                {
                    if(urlIdPerm2 == null) continue;
                    string firstPermission = url1 == "" | url1 == null ? "null" : url1 ;
                    string secondPermission = url2 == "" | url2 == null ? "null" : url2;
                    try
                    {
            
                        urlIdPerm3 = (UrlIdentityPermission)urlIdPerm1.Intersect(urlIdPerm2);
                        if (urlIdPerm3 != null && urlIdPerm3.Url != null)
                        {

                            Console.WriteLine("The intersection of " + firstPermission 
                                + "  and \n\t"  + secondPermission + 
                                " = \n\t" + ((UrlIdentityPermission)urlIdPerm3).Url.ToString());
                        }
                        else
                        {
                            Console.WriteLine("The intersection of " + firstPermission + "  and \n\t"  
                                + secondPermission + " is null. ");
                        }
                    }
                    catch(Exception e) 
                    {
                        Console.WriteLine("An exception was thrown for the intersection : "  + e);
                        returnValue=false;
                    }
                }
            }

            return returnValue;

        }
    //Copy creates and returns an identical copy of the current permission.
        private bool CopyDemo()
        {
    
            bool returnValue = true;
            string url1;
            UrlIdentityPermission urlIdPerm1,urlIdPerm2;
            UrlGenerator urlGen1 = new UrlGenerator();
            UrlGenerator urlGen2 = new UrlGenerator();

            urlGen1.ResetIndex();
            while(urlGen1.CreateUrl(out urlIdPerm1, out url1)) 
            {
                if(urlIdPerm1 == null ) continue;  
                urlGen2.ResetIndex();
                Console.WriteLine("********************************************************\n");
                try
                {            
                    urlIdPerm2 = (UrlIdentityPermission)urlIdPerm1.Copy();
                    if (urlIdPerm2 != null)
                    {
                        Console.WriteLine("Result of copy = " + urlIdPerm2.ToString() + "\n");
                    }
                    else
                    {
                        Console.WriteLine("Result of copy is null. \n");
                    }
                }
                catch(Exception e) 
                {
                    {
                        if (url1 == "")
                        {
                            Console.WriteLine( "The target UrlIdentityPermission is empty; copy failed.");

                        }
                        else
                        Console.WriteLine( 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;

            string url1;
            UrlIdentityPermission urlIdPerm1,urlIdPerm2;

            UrlGenerator urlGen1 = new UrlGenerator();
            UrlGenerator urlGen2 = new UrlGenerator();

            urlGen1.ResetIndex();
            while(urlGen1.CreateUrl(out urlIdPerm1, out url1)) 
            {
                if(urlIdPerm1 == null) continue;
                Console.WriteLine("********************************************************\n");
                urlGen2.ResetIndex();
                try
                {            
                    urlIdPerm2= new UrlIdentityPermission(PermissionState.None);
                    urlIdPerm2.FromXml(urlIdPerm1.ToXml());
                    Console.WriteLine("Result of ToFromXml = " +urlIdPerm2.ToString() + "\n");
            
                }
                catch(Exception e) 
                {
                    Console.WriteLine("ToFromXml failed :" + urlIdPerm1.ToString() + e);
                    continue;
                }
            }

            return returnValue;

        }
        // Invoke all demos.
        public bool runDemo()
        {

            bool ret=true;
            bool retTmp;
            // Call IsSubsetOf demo.
            retTmp= IsSubsetOfDemo();
            if(retTmp)    Console.Out.WriteLine("IsSubset demo completed successfully.");
            else Console.Out.WriteLine("IsSubset demo failed.");
            ret=retTmp && ret;
    
            // Call Union demo.
            retTmp= UnionDemo();
            if(retTmp) Console.Out.WriteLine("Union demo completed successfully.");
            else Console.Out.WriteLine("Union demo failed.");
            ret=retTmp && ret;

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


            // Call Copy demo.    
            retTmp= CopyDemo();
            if(retTmp) Console.Out.WriteLine("Copy demo completed successfully.");
            else Console.Out.WriteLine("Copy demo failed.");
            ret=retTmp && ret;
        
            // Call ToFromXml demo.
            retTmp= ToFromXmlDemo();
            if(retTmp)Console.Out.WriteLine("ToFromXml demo completed successfully.");
            else Console.Out.WriteLine("ToFromXml demo failed.");
            ret=retTmp && ret;

            return (  ret );    

        }
// Test harness.
        public static void Main(String[] args) 
        {
            try
            {
                UrlIdentityPermissionDemo democase = new UrlIdentityPermissionDemo();
                bool ret = democase.runDemo();
                if (ret)
                {
                    Console.Out.WriteLine("UrlIdentity demo completed successfully.");
                    Console.Out.WriteLine("Press the Enter key to exit.");
                    string consoleInput = Console.ReadLine();
                    System.Environment.ExitCode = 100;
                }
                else
                {
                    Console.Out.WriteLine("UrlIdentity 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("UrlIdentity 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 UrlIdentityPermission objects.
 
    internal  class UrlGenerator
    {


        private string[] myUrl = 
    {"https://www.fourthcoffee.com/process/grind.htm","https://www.fourthcoffee.com/process/*",
        "https://www.fourthcoffee.com/*","https://www.fourthcoffee.com/process/*","https://www.contoso.com/math.asmx"};

        private int urlIndex = 0;

        public UrlGenerator()
        {
            ResetIndex();
        }

        public void ResetIndex()
        {
            urlIndex = 0;
        }
        // CreateUrl creates a UrlIdentityPermission.

        public bool CreateUrl(out UrlIdentityPermission urlPerm, out string url)
        {


            if(urlIndex == myUrl.Length) 
            {
                urlPerm = new UrlIdentityPermission(PermissionState.None);
                url="";
                urlIndex++;
                return true;

            }
            if(urlIndex > myUrl.Length)
            {
                urlPerm = null;
                url = "";
                return false;
            }

            url = myUrl[urlIndex++];

            try
            {
                urlPerm = new UrlIdentityPermission(url);
                return true;
            } 
            catch(Exception e)
            {
                Console.WriteLine("Cannot create UrlIdentityPermission : " + url +" "+e);
                urlPerm = new UrlIdentityPermission(PermissionState.None);
                url="";
                return true;
            }
        }
    } // End of UrlGenerator.


[C++] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods
// of the UrlIdentityPermission 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 UrlIdentityPermission objects.

private __gc class UrlGenerator {
private:
   String* myUrl[];

private:
   int urlIndex;

public:
   UrlGenerator() {
      String* temp0 [] = {
         S"https://www.fourthcoffee.com/process/grind.htm",
            S"https://www.fourthcoffee.com/process/*",
            S"https://www.fourthcoffee.com/*",
            S"https://www.fourthcoffee.com/process/*",
            S"https://www.contoso.com/math.asmx"
      };
      myUrl = temp0;
      ResetIndex();
   }

public:
   void ResetIndex() {
      urlIndex = 0;
   }
   // CreateUrl creates a UrlIdentityPermission.
public:
   bool CreateUrl([Out] UrlIdentityPermission** urlPerm, [Out] String** url) {
      if (urlIndex == myUrl->Length) {
         *urlPerm = new UrlIdentityPermission(PermissionState::None);
         *url=S"";
         urlIndex++;
         return true;
      }
      if (urlIndex > myUrl->Length) {
         *urlPerm = 0;
         *url = S"";
         return false;
      }

      *url = myUrl[urlIndex++];

      try {
         *urlPerm = new UrlIdentityPermission(*url);
         return true;
      } catch (Exception* e) {
         Console::WriteLine(S"Cannot create UrlIdentityPermission : {0} {1}", *url, e);
         *urlPerm = new UrlIdentityPermission(PermissionState::None);
         *url=S"";
         return false;
      }
   }
}; // End of UrlGenerator.

public __gc class UrlIdentityPermissionDemo {
   // IsSubsetOf determines whether the current permission is a subset of the specified permission.
private:
   bool IsSubsetOfDemo() {

      bool returnValue = true;

      String* url1, *url2;
      UrlIdentityPermission* urlIdPerm1, *urlIdPerm2;

      UrlGenerator* urlGen1 = new UrlGenerator();
      UrlGenerator* urlGen2 = new UrlGenerator();

      urlGen1->ResetIndex();
      while(urlGen1->CreateUrl(&urlIdPerm1, &url1)) {
         if (urlIdPerm1 == 0) continue;
         urlGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");
         while(urlGen2->CreateUrl(&urlIdPerm2, &url2)) {
            String* firstPermission = url1->Equals(S"") || url1 == 0 ? S"null" : url1 ;
            String* secondPermission = url2->Equals(S"") || url2 == 0 ? S"null" : url2;
            if (urlIdPerm2 == 0) continue;
            try {
               if (urlIdPerm1->IsSubsetOf(urlIdPerm2)) {

                  Console::WriteLine(S"{0}\n is a subset of {1}\n",
                     firstPermission, secondPermission);
               } else {
                  Console::WriteLine(S"{0}\n is not a subset of {1}\n",
                     firstPermission, secondPermission);
               }
            } catch (Exception* e) {
               Console::WriteLine(S"An exception was thrown for subset : {0}\n{1}\n{2}",
                  url1->Equals(S"") ? S"0." : url1, url2->Equals(S"") ? S"0." : url2, e);
            }
         }
      }
      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;

      String* url1, *url2;
      UrlIdentityPermission* urlIdPerm1, *urlIdPerm2;
      IPermission* urlIdPerm3;

      UrlGenerator* urlGen1 = new UrlGenerator();
      UrlGenerator* urlGen2 = new UrlGenerator();

      urlGen1->ResetIndex();
      while(urlGen1->CreateUrl(&urlIdPerm1, &url1)) {
         if (urlIdPerm1 == 0) continue;

         Console::WriteLine(S"**********************************************************\n");
         urlGen2->ResetIndex();
         while(urlGen2->CreateUrl(&urlIdPerm2, &url2)) {
            try {
               if (urlIdPerm2 == 0) continue;

               String* firstPermission = url1->Equals(S"") || url1 == 0 ? S"null" : url1 ;
               String* secondPermission = url2->Equals(S"") || url2 == 0 ? S"null" : url2;
               urlIdPerm3 = dynamic_cast<UrlIdentityPermission*>(urlIdPerm1->Union(urlIdPerm2));
               urlIdPerm3 = urlIdPerm1->Union(urlIdPerm2);

               if (urlIdPerm3 == 0) {
                  Console::WriteLine(S"The union of {0}  and \n\t{1} is null.",
                     firstPermission,
                     secondPermission);
               } else {
                  Console::WriteLine(S"The union of {0}  and \n\t{1} = \n\t{2}",
                     firstPermission,
                     secondPermission,
                     (dynamic_cast<UrlIdentityPermission*>(urlIdPerm3))->Url);
               }
            } catch (Exception* e) {
               Console::WriteLine(S"An exception was thrown for union : {0}", e);
               returnValue=false;
            }
         }
      }
      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;

      String* url1, *url2;
      UrlIdentityPermission* urlIdPerm1, *urlIdPerm2, *urlIdPerm3;

      UrlGenerator* urlGen1 = new UrlGenerator();
      UrlGenerator* urlGen2 = new UrlGenerator();

      urlGen1->ResetIndex();
      while(urlGen1->CreateUrl(&urlIdPerm1, &url1)) {
         if (urlIdPerm1 == 0) continue;
         Console::WriteLine(S"**********************************************************\n");
         urlGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");
         while(urlGen2->CreateUrl(&urlIdPerm2, &url2)) {
            if (urlIdPerm2 == 0) continue;
            String* firstPermission = url1->Equals(S"") || url1 == 0 ? S"null" : url1 ;
            String* secondPermission = url2->Equals(S"") || url2 == 0 ? S"null" : url2;
            try {
               urlIdPerm3 = dynamic_cast<UrlIdentityPermission*>(urlIdPerm1->Intersect(urlIdPerm2));
               if (urlIdPerm3 != 0 && urlIdPerm3->Url != 0) {
                  Console::WriteLine(S"The intersection of {0}  and \n\t{1} = \n\t{2}",
                     firstPermission,
                     secondPermission,
                     (dynamic_cast<UrlIdentityPermission*>(urlIdPerm3))->Url);
               } else {
                  Console::WriteLine(S"The intersection of {0}  and \n\t{1} is null. ",
                     firstPermission,
                     secondPermission);
               }
            } catch (Exception* e) {
               Console::WriteLine(S"An exception was thrown for the intersection : {0}", e);
               returnValue=false;
            }
         }
      }
      return returnValue;
   }
   //Copy creates and returns an identical copy of the current permission.
private:
   bool CopyDemo() {

      bool returnValue = true;
      String* url1;
      UrlIdentityPermission* urlIdPerm1, *urlIdPerm2;
      UrlGenerator* urlGen1 = new UrlGenerator();
      UrlGenerator* urlGen2 = new UrlGenerator();

      urlGen1->ResetIndex();
      while(urlGen1->CreateUrl(&urlIdPerm1, &url1)) {
         if (urlIdPerm1 == 0) continue;
         urlGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");
         try {
            urlIdPerm2 = dynamic_cast<UrlIdentityPermission*>(urlIdPerm1->Copy());
            if (urlIdPerm2 != 0) {
               Console::WriteLine(S"Result of copy = {0}\n", urlIdPerm2);
            } else {
               Console::WriteLine(S"Result of copy is null. \n");
            }
         } catch (Exception* e) { {
            if (url1->Equals(S"")) {
               Console::WriteLine(S"The target UrlIdentityPermission is empty; copy failed.");
            } else
               Console::WriteLine(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;

      String* url1;
      UrlIdentityPermission* urlIdPerm1, *urlIdPerm2;

      UrlGenerator* urlGen1 = new UrlGenerator();
      UrlGenerator* urlGen2 = new UrlGenerator();

      urlGen1->ResetIndex();
      while(urlGen1->CreateUrl(&urlIdPerm1, &url1)) {
         if (urlIdPerm1 == 0) continue;
         Console::WriteLine(S"********************************************************\n");
         urlGen2->ResetIndex();
         try {
            urlIdPerm2 = new UrlIdentityPermission(PermissionState::None);
            urlIdPerm2->FromXml(urlIdPerm1->ToXml());
            Console::WriteLine(S"Result of ToFromXml = {0}\n", urlIdPerm2);
         } catch (Exception* e) {
            Console::WriteLine(S"ToFromXml failed : {0}{1}", urlIdPerm1, e);
            continue;
         }
      }
      return returnValue;
   }
   // Invoke all demos.
public:
   bool runDemo() {

      bool ret=true;
      bool retTmp;
      // Call IsSubsetOf demo.
      retTmp= IsSubsetOfDemo();
      if (retTmp)
         Console::WriteLine(S"IsSubset demo completed successfully.");
      else 
         Console::WriteLine(S"IsSubset demo failed.");
      ret=retTmp && ret;

      // Call Union demo.
      retTmp= UnionDemo();
      if (retTmp)
         Console::WriteLine(S"Union demo completed successfully.");
      else
         Console::WriteLine(S"Union demo failed.");
      ret=retTmp && ret;

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

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

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

      return (ret);
   }
};

// Test harness.
int main() {
   try {
      UrlIdentityPermissionDemo* democase = new UrlIdentityPermissionDemo();
      bool ret = democase->runDemo();
      if (ret) {
         Console::WriteLine(S"UrlIdentity demo completed successfully.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 100;
      } else {
         Console::WriteLine(S"UrlIdentity demo failed.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 101;
      }
   } catch (Exception* e) {
      Console::WriteLine(S"UrlIdentity 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 内)

参照

UrlIdentityPermission メンバ | System.Security.Permissions 名前空間 | アクセス許可 | アクセス許可の要求 | UrlIdentityPermissionAttribute | Url | UrlMembershipCondition