UrlIdentityPermission 클래스
코드가 발생되는 URL의 ID 권한을 정의합니다. 이 클래스는 상속될 수 없습니다.
네임스페이스: System.Security.Permissions
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class UrlIdentityPermission
Inherits CodeAccessPermission
‘사용 방법
Dim instance As UrlIdentityPermission
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class UrlIdentityPermission : CodeAccessPermission
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class UrlIdentityPermission sealed : public CodeAccessPermission
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public final class UrlIdentityPermission extends CodeAccessPermission
SerializableAttribute
ComVisibleAttribute(true)
public final class UrlIdentityPermission extends CodeAccessPermission
설명
프로토콜(HTTP, HTTPS, FTP)과 파일을 포함한 전체 URL이 고려됩니다. 예를 들어, https://www.fourthcoffee.com/process/grind.htm은 전체 URL입니다.
URL을 정확하게 일치시키거나 https://www.fourthcoffee.com/process/\*와 같이 마지막 위치에 와일드카드를 사용할 수 있습니다. URL에는 점 구분 기호 앞에도 와일드카드("*")를 사용할 수 있습니다. 예를 들어, URL 이름 문자열 https://www.fourthcoffee.com/process/grind.htm/은 http://*.fourthcoffee.com/process/grind.htm/과 http://*.com/process/grind.htm/의 하위 집합입니다.
경고
UrlIdentityPermission은 URL과 IP 주소를 포함하여 파일의 모든 경로에 대한 사용 권한을 부여합니다. 파일에 대한 액세스를 Deny하려면 해당 파일에 대해 가능한 모든 경로를 Deny해야 합니다. 예를 들어 https://www.fourthcoffee.com/process/grind.htm의 IP 주소가 192.168.238.241인 경우 https://www.fourthcoffee.com/process/grind.htm에 대한 액세스를 Deny하려면 https://www.fourthcoffee.com/process/grind.htm, 192.168.238.241/grind.htm 및 해당 코드에 액세스할 수 있는 그 밖의 모든 경로를 Deny해야 합니다. 불행하게도, URL 경로를 나타낼 수 있는 방법은 무수히 많습니다. 따라서 Deny만 사용해서 모든 경로를 차단하는 것은 매우 어려운 일입니다. 여러 경로를 처리하는 더 좋은 방법은 PermitOnly와 Deny를 결합하여 사용하는 것입니다. PermitOnly를 사용하여 액세스할 수 있는 일정한 URL의 집합을 확인한 다음 Deny를 사용하여 해당 집합에서 액세스를 거부할 주소를 명시적으로 선택하면 됩니다. 이 주제 및 Deny와 함께 PermitOnly를 사용하는 방법에 대한 자세한 내용은 Deny 메서드 사용 항목의 Canonicalization Problems Using Deny를 참조하십시오.
참고
.NET Framework 버전 1.0 및 1.1에서 ID 권한은 Unrestricted 권한 상태 값을 가질 수 없습니다. .NET Framework 버전 2.0에서 ID 권한은 모든 권한 상태 값을 가질 수 있습니다. 즉, 버전 2.0에서 ID 권한의 동작은 IUnrestrictedPermission 인터페이스를 구현하는 권한의 동작과 같습니다. 버전 1.1 CAS 정책을 사용하여 버전 2.0 응용 프로그램을 실행하는 데 대한 자세한 내용은 <legacyV1CASPolicy> 요소를 참조하십시오.
예제
다음 예제에서는 UrlIdentityPermission 메서드의 동작을 보여 줍니다. 이 샘플은 메서드의 사용 방법을 보여 주기 위한 것이 아니라 메서드의 결과를 보여 주기 위한 것입니다.
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports Microsoft.VisualBasic
<Assembly: CLSCompliant(True)>
Public Class UrlIdentityPermissionDemo
Private Function IsSubsetOfDemo() As Boolean
Dim returnValue As Boolean = True
Dim url1 As String = ""
Dim url2 As String = ""
Dim urlIdPerm1, urlIdPerm2 As UrlIdentityPermission
Dim urlGen1 As New UrlGenerator()
Dim urlGen2 As New UrlGenerator()
urlGen1.ResetIndex()
While urlGen1.CreateUrl(url1)
urlIdPerm1 = New UrlIdentityPermission(url1)
urlGen2.ResetIndex()
Console.WriteLine("********************************************************\n")
While urlGen2.CreateUrl(url2)
urlIdPerm2 = New UrlIdentityPermission(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 As String = ""
Dim 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(url1)
urlIdPerm1 = New UrlIdentityPermission(url1)
Console.WriteLine("**********************************************************" & ControlChars.Lf)
urlGen2.ResetIndex()
While urlGen2.CreateUrl(url2)
Try
urlIdPerm2 = New UrlIdentityPermission(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)
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
'Expected exception.
Console.WriteLine("An exception was thrown for union :" & e.ToString())
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 As String = ""
Dim url2 As String = ""
Dim urlIdPerm1, urlIdPerm2, urlIdPerm3 As UrlIdentityPermission
Dim urlGen1 As New UrlGenerator()
Dim urlGen2 As New UrlGenerator()
urlGen1.ResetIndex()
While urlGen1.CreateUrl(url1)
urlIdPerm1 = New UrlIdentityPermission(url1)
Console.WriteLine("**********************************************************" & ControlChars.Lf)
urlGen2.ResetIndex()
Console.WriteLine("********************************************************\n")
While urlGen2.CreateUrl(url2)
urlIdPerm2 = New UrlIdentityPermission(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)
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 url2 As String = ""
Dim urlIdPerm1, urlIdPerm2 As UrlIdentityPermission
Dim urlGen1 As New UrlGenerator()
Dim urlGen2 As New UrlGenerator()
urlGen1.ResetIndex()
While urlGen1.CreateUrl(url1)
urlIdPerm1 = New UrlIdentityPermission(url1)
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(url1)
urlIdPerm1 = New UrlIdentityPermission(url1)
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 url As String) As Boolean
If urlIndex = myUrl.Length Then
url = ""
urlIndex &= 1
Return True
End If
If urlIndex > myUrl.Length Then
url = ""
Return False
End If
url = myUrl(urlIndex)
urlIndex = urlIndex + 1
Return True
End Function 'CreateUrl
' End of UrlGenerator.
using System;
using System.Security;
using System.Security.Permissions;
public class UrlIdentityPermissionDemo
{
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 url1))
{
urlIdPerm1 = new UrlIdentityPermission(url1);
Console.WriteLine("********************************************************\n");
while (urlGen2.CreateUrl(out url2))
{
urlIdPerm2 = new UrlIdentityPermission(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 url1))
{
urlIdPerm1 = new UrlIdentityPermission(url1);
Console.WriteLine("**********************************************************\n");
urlGen2.ResetIndex();
while (urlGen2.CreateUrl(out url2))
{
try
{
urlIdPerm2 = new UrlIdentityPermission(url2);
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)
{ //Expected exception.
Console.WriteLine("An exception was thrown for union :" + e.Message);
}
}
}
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 url1))
{
urlIdPerm1 = new UrlIdentityPermission(url1);
Console.WriteLine("**********************************************************\n");
urlGen2.ResetIndex();
Console.WriteLine("********************************************************\n");
while (urlGen2.CreateUrl(out url2))
{
urlIdPerm2 = new UrlIdentityPermission(url2);
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 url1))
{
urlIdPerm1 = new UrlIdentityPermission(url1);
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 url1))
{
urlIdPerm1 = new UrlIdentityPermission(url1);
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 string url)
{
if (urlIndex == myUrl.Length)
{
url = "";
urlIndex++;
return true;
}
if (urlIndex > myUrl.Length)
{
url = "";
return false;
}
url = myUrl[urlIndex++];
return true;
}
} // End of UrlGenerator.
// This sample demonstrates the IsSubsetOf, Union, Intersect,
// Copy, ToXml and FromXml methods
// of the UrlIdentityPermission class.
import System.*;
import System.Security.*;
import System.Security.Permissions.*;
/** @assembly CLSCompliant(true)
*/
public class UrlIdentityPermissionDemo
{
// IsSubsetOf determines whether the current permission is a
// subset of the specified permission.
private boolean IsSubsetOfDemo()
{
boolean returnValue = true;
String url1[] = new String[1];
String url2[] = new String[1];
UrlIdentityPermission urlIdPerm1[] = new UrlIdentityPermission[1];
UrlIdentityPermission urlIdPerm2[] = new UrlIdentityPermission[1];
UrlGenerator urlGen1 = new UrlGenerator();
UrlGenerator urlGen2 = new UrlGenerator();
urlGen1.ResetIndex();
while (urlGen1.CreateUrl(urlIdPerm1, url1)) {
if (urlIdPerm1 == null) {
continue;
}
urlGen2.ResetIndex();
Console.WriteLine("******************************************"
+ "**************\n");
while (urlGen2.CreateUrl(urlIdPerm2, url2)) {
System.String firstPermission = (url1[0].Equals("")
|| url1[0] == null) ? "null" : url1[0];
System.String secondPermission = (url2[0].Equals("")
|| url2[0] == null) ? "null" : url2[0];
if (urlIdPerm2 == null) {
continue;
}
try {
if (urlIdPerm1[0].IsSubsetOf(urlIdPerm2[0])) {
Console.WriteLine((firstPermission
+ "\n is a subset of " + secondPermission + "\n"));
}
else {
Console.WriteLine((firstPermission
+ "\n is not a subset of " + secondPermission
+ "\n"));
}
}
catch (System.Exception e) {
Console.WriteLine(("An exception was thrown for subset :"
+ url1 == "") ? "null." : (url1 + "\n"
+ url2 == "") ? "null." : url2 + "\n" + e);
}
}
}
return returnValue;
} //IsSubsetOfDemo
// Union creates a new permission that is the union
// of the current permission and
// the specified permission.
private boolean UnionDemo()
{
boolean returnValue = true;
String url1[] = new String[1];
String url2[] = new String[1];
UrlIdentityPermission urlIdPerm1[] = new UrlIdentityPermission[1];
UrlIdentityPermission urlIdPerm2[] = new UrlIdentityPermission[1];
IPermission urlIdPerm3;
UrlGenerator urlGen1 = new UrlGenerator();
UrlGenerator urlGen2 = new UrlGenerator();
urlGen1.ResetIndex();
while (urlGen1.CreateUrl(urlIdPerm1, url1)) {
if (urlIdPerm1 == null) {
continue;
}
Console.WriteLine("******************************************"
+ "****************\n");
urlGen2.ResetIndex();
while (urlGen2.CreateUrl(urlIdPerm2, url2)) {
try {
if (urlIdPerm2 == null) {
continue;
}
String firstPermission = (url1[0] == ""
|| url1[0] == null) ? "null" : url1[0];
String secondPermission = (url2[0] == ""
|| url2[0] == null) ? "null" : url2[0];
urlIdPerm3 = ((UrlIdentityPermission)(urlIdPerm1[0].
Union(urlIdPerm2[0])));
urlIdPerm3 = urlIdPerm1[0].Union(urlIdPerm2[0]);
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)).
get_Url().ToString()));
}
}
catch (System.Exception e) {
Console.WriteLine(("An exception was thrown for union :"
+ e));
returnValue = false;
}
}
}
return returnValue;
} //UnionDemo
// Intersect creates and returns a new permission that is
// the intersection of the
// current permission and the permission specified.
private boolean IntersectDemo()
{
boolean returnValue = true;
String url1[] = new String[1];
String url2[] = new String[1];
UrlIdentityPermission urlIdPerm1[] = new UrlIdentityPermission[1];
UrlIdentityPermission urlIdPerm2[] = new UrlIdentityPermission[1];
UrlIdentityPermission urlIdPerm3[] = new UrlIdentityPermission[1];
UrlGenerator urlGen1 = new UrlGenerator();
UrlGenerator urlGen2 = new UrlGenerator();
urlGen1.ResetIndex();
while (urlGen1.CreateUrl(urlIdPerm1, url1)) {
if (urlIdPerm1 == null) {
continue;
}
Console.WriteLine("***************************************"
+ "*******************\n");
urlGen2.ResetIndex();
Console.WriteLine("****************************************"
+ "****************\n");
while (urlGen2.CreateUrl(urlIdPerm2, url2)) {
if (urlIdPerm2 == null) {
continue;
}
String firstPermission = (url1[0] == ""
|| url1[0] == null) ? "null" : url1[0];
String secondPermission = (url2[0] == ""
|| url2[0] == null) ? "null" : url2[0];
try {
urlIdPerm3[0] = ((UrlIdentityPermission)(urlIdPerm1[0].
Intersect(urlIdPerm2[0])));
if (urlIdPerm3[0] != null && urlIdPerm3[0].
get_Url() != null) {
Console.WriteLine(("The intersection of "
+ firstPermission + " and \n\t" + secondPermission
+ " = \n\t"
+ ((UrlIdentityPermission)(urlIdPerm3[0])).
get_Url().ToString()));
}
else {
Console.WriteLine(("The intersection of "
+ firstPermission + " and \n\t"
+ secondPermission + " is nu //IntersectDemoll. "));
}
}
catch (System.Exception e) {
Console.WriteLine(("An exception was thrown "
+ "for the intersection : " + e));
returnValue = false;
}
}
}
return returnValue;
} //IntersectDemo
//Copy creates and returns an identical copy of the current permission.
private boolean CopyDemo()
{
boolean returnValue = true;
String url1[] = new String[1];
UrlIdentityPermission urlIdPerm1[] = new UrlIdentityPermission[1];
UrlIdentityPermission urlIdPerm2[] = new UrlIdentityPermission[1];
UrlGenerator urlGen1 = new UrlGenerator();
UrlGenerator urlGen2 = new UrlGenerator();
urlGen1.ResetIndex();
while (urlGen1.CreateUrl(urlIdPerm1, url1)) {
if (urlIdPerm1 == null) {
continue;
}
urlGen2.ResetIndex();
Console.WriteLine("**********************************************"
+ "**********\n");
try {
urlIdPerm2[0] = ((UrlIdentityPermission)(urlIdPerm1[0].Copy()));
if (urlIdPerm2 != null) {
Console.WriteLine(("Result of copy = " + urlIdPerm2[0].
ToString() + "\n"));
}
else {
Console.WriteLine("Result of copy is null. \n");
}
}
catch (System.Exception e) {
{
if (url1[0].Equals("")) {
Console.WriteLine("The target UrlIdentityPermission"
+ " is empty; copy failed.");
}
else {
Console.WriteLine(e);
}
}
continue;
}
}
return returnValue;
} //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 boolean ToFromXmlDemo()
{
boolean returnValue = true;
String url1[] = new String[1];
UrlIdentityPermission urlIdPerm1[] = new UrlIdentityPermission[1];
UrlIdentityPermission urlIdPerm2[] = new UrlIdentityPermission[1];
UrlGenerator urlGen1 = new UrlGenerator();
UrlGenerator urlGen2 = new UrlGenerator();
urlGen1.ResetIndex();
while (urlGen1.CreateUrl(urlIdPerm1, url1)) {
if (urlIdPerm1 == null) {
continue;
}
Console.WriteLine("********************************************"
+ "************\n");
urlGen2.ResetIndex();
try {
urlIdPerm2[0] = new UrlIdentityPermission(PermissionState.None);
urlIdPerm2[0].FromXml(urlIdPerm1[0].ToXml());
Console.WriteLine(("Result of ToFromXml = "
+ urlIdPerm2[0].ToString() + "\n"));
}
catch (System.Exception e) {
Console.WriteLine(("ToFromXml failed :"
+ urlIdPerm1.ToString() + e));
continue;
}
}
return returnValue;
} //ToFromXmlDemo
// Invoke all demos.
public boolean runDemo()
{
boolean ret = true;
boolean retTmp;
// Call IsSubsetOf demo.
retTmp = IsSubsetOfDemo();
if (retTmp) {
Console.get_Out().WriteLine("IsSubset demo completed "
+ "successfully.");
}
else {
Console.get_Out().WriteLine("IsSubset demo failed.");
}
ret = retTmp && ret;
// Call Union demo.
retTmp = UnionDemo();
if (retTmp) {
Console.get_Out().WriteLine("Union demo completed successfully.");
}
else {
Console.get_Out().WriteLine("Union demo failed.");
}
ret = retTmp && ret;
// Call Intersect demo.
retTmp = IntersectDemo();
if (retTmp) {
Console.get_Out().WriteLine("Intersect demo completed "
+ "successfully.");
}
else {
Console.get_Out().WriteLine("Intersect demo failed.");
}
ret = retTmp && ret;
// Call Copy demo.
retTmp = CopyDemo();
if (retTmp) {
Console.get_Out().WriteLine("Copy demo completed successfully.");
}
else {
Console.get_Out().WriteLine("Copy demo failed.");
}
ret = retTmp && ret;
// Call ToFromXml demo.
retTmp = ToFromXmlDemo();
if (retTmp) {
Console.get_Out().WriteLine("ToFromXml demo "
+ "completed successfully.");
}
else {
Console.get_Out().WriteLine("ToFromXml demo failed.");
}
ret = retTmp && ret;
return ret;
} //runDemo
// Test harness.
public static void main(String[] args)
{
try {
UrlIdentityPermissionDemo democase = new
UrlIdentityPermissionDemo();
boolean ret = democase.runDemo();
if (ret) {
Console.get_Out().WriteLine("UrlIdentity demo completed"
+ " successfully.");
Console.get_Out().WriteLine("Press the Enter key to exit.");
String consoleInput = Console.ReadLine();
Environment.set_ExitCode(100);
}
else {
Console.get_Out().WriteLine("UrlIdentity demo failed.");
Console.get_Out().WriteLine("Press the Enter key to exit.");
String consoleInput = Console.ReadLine();
Environment.set_ExitCode(101);
}
}
catch (System.Exception e) {
Console.get_Out().WriteLine("UrlIdentity demo failed.");
Console.WriteLine(e.ToString());
Console.get_Out().WriteLine("Press the Enter key to exit.");
String consoleInput = Console.ReadLine();
Environment.set_ExitCode(101);
}
} //main
} //UrlIdentityPermissionDemo
// This class generates UrlIdentityPermission objects.
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();
} //UrlGenerator
public void ResetIndex()
{
urlIndex = 0;
} //ResetIndex
// CreateUrl creates a UrlIdentityPermission.
public boolean CreateUrl(UrlIdentityPermission urlPerm[], String url[])
{
UrlIdentityPermission urlP = null;
if (urlIndex == myUrl.length) {
urlPerm[0] = new UrlIdentityPermission(PermissionState.None);
url[0] = "";
urlIndex++;
return true;
}
if (urlIndex > myUrl.length) {
urlPerm = null;
url[0] = "";
return false;
}
url[0] = myUrl[urlIndex++];
try {
urlP = new UrlIdentityPermission(url[0]);
urlPerm[0] = urlP;
return true;
}
catch (System.Exception e) {
Console.WriteLine(("Cannot create UrlIdentityPermission : "
+ url + " " + e));
urlPerm[0] = new UrlIdentityPermission(PermissionState.None);
url[0] = "";
return true;
}
} //CreateUrl
} //UrlGenerator
상속 계층 구조
System.Object
System.Security.CodeAccessPermission
System.Security.Permissions.UrlIdentityPermission
스레드로부터의 안전성
이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.
플랫폼
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
참고 항목
참조
UrlIdentityPermission 멤버
System.Security.Permissions 네임스페이스
UrlIdentityPermissionAttribute
Url
UrlMembershipCondition