PublisherIdentityPermission 클래스
소프트웨어 게시자의 ID를 나타냅니다. 이 클래스는 상속될 수 없습니다.
네임스페이스: System.Security.Permissions
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class PublisherIdentityPermission
Inherits CodeAccessPermission
‘사용 방법
Dim instance As PublisherIdentityPermission
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class PublisherIdentityPermission : CodeAccessPermission
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class PublisherIdentityPermission sealed : public CodeAccessPermission
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public final class PublisherIdentityPermission extends CodeAccessPermission
SerializableAttribute
ComVisibleAttribute(true)
public final class PublisherIdentityPermission extends CodeAccessPermission
예제
다음 코드 예제에서는 PublisherIdentityPermission 메서드의 동작을 보여 줍니다. 이 샘플은 사용 권한의 사용 방법을 보여 주기 위한 것이 아닙니다. PublisherIdentityPermission의 사용 방법을 보여 주는 예제는 PublisherIdentityPermission를 참조하십시오.
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Cryptography.X509Certificates
Imports System.IO
Imports Microsoft.VisualBasic
Public Class PublisherIdentityDemo
' IsSubsetOf determines whether the current permission is a subset of the specified permission.
Private Function IsSubsetOfDemo() As Boolean
Dim retCode As Boolean = True
Dim publisher1, publisher2 As [String]
Dim pubIdPerm1, pubIdPerm2 As PublisherIdentityPermission
Dim pubGen1 As New PublisherGenerator()
Dim pubGen2 As New PublisherGenerator()
pubGen1.ResetIndex()
While pubGen1.CreatePublisher(pubIdPerm1, publisher1)
If pubIdPerm1 Is Nothing Then
GoTo ContinueWhile1
End If
pubGen2.ResetIndex()
While pubGen2.CreatePublisher(pubIdPerm2, publisher2)
If pubIdPerm2 Is Nothing Then
GoTo ContinueWhile2
End If
Try
' Check for whether either of the publishers is null.
' An exception will be thrown if either is null.
If publisher1 <> "null" AndAlso publisher2 <> "null" Then
If pubIdPerm1.IsSubsetOf(pubIdPerm2) Then
Console.WriteLine((pubIdPerm1.Certificate.GetName() & " is a subset of " & pubIdPerm2.Certificate.GetName() & ControlChars.Lf))
Else
Console.WriteLine((pubIdPerm1.Certificate.GetName() & " is not a subset of " & pubIdPerm2.Certificate.GetName() & ControlChars.Lf))
End If
End If
Catch
Console.WriteLine(("An exception was thrown for " & publisher1 & " is a subset of " & publisher2 & ControlChars.Lf & "."))
retCode = False
End Try
ContinueWhile2:
End While
ContinueWhile1:
End While
Return retCode
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 retCode As Boolean = True
Dim publisher1, publisher2 As [String]
Dim pubIdPerm1, pubIdPerm2 As PublisherIdentityPermission
Dim p3 As IPermission
Dim pubGen1 As New PublisherGenerator()
Dim pubGen2 As New PublisherGenerator()
pubGen1.ResetIndex()
While pubGen1.CreatePublisher(pubIdPerm1, publisher1)
If pubIdPerm1 Is Nothing Or pubIdPerm1.Certificate Is Nothing Then
GoTo ContinueWhile1
End If
pubGen2.ResetIndex()
Try
While pubGen2.CreatePublisher(pubIdPerm2, publisher2)
If pubIdPerm2 Is Nothing Or pubIdPerm2.Certificate Is Nothing Then
GoTo ContinueWhile2
End If
p3 = pubIdPerm1.Union(pubIdPerm2)
If Not (p3 Is Nothing) Then
Console.WriteLine(("The union of " & pubIdPerm1.Certificate.GetName() & " and " & pubIdPerm2.Certificate.GetName() & " = " & CType(p3, PublisherIdentityPermission).Certificate.GetName()))
Else
Console.WriteLine(("The union of " & pubIdPerm1.Certificate.GetName() & " and " & pubIdPerm2.Certificate.GetName() & " = null."))
End If
ContinueWhile2:
End While
Catch e As Exception
Console.WriteLine(e.Message)
End Try
ContinueWhile1:
End While
Return retCode
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 retCode As Boolean = True
Dim publisher1, publisher2 As [String]
Dim pubIdPerm1, pubIdPerm2, p3 As PublisherIdentityPermission
Dim pubGen1 As New PublisherGenerator()
Dim pubGen2 As New PublisherGenerator()
pubGen1.ResetIndex()
While pubGen1.CreatePublisher(pubIdPerm1, publisher1)
If pubIdPerm1 Is Nothing Or pubIdPerm1.Certificate Is Nothing Then
GoTo ContinueWhile1
End If
pubGen2.ResetIndex()
While pubGen2.CreatePublisher(pubIdPerm2, publisher2)
If pubIdPerm2 Is Nothing Or pubIdPerm2.Certificate Is Nothing Then
GoTo ContinueWhile2
End If
p3 = CType(pubIdPerm1.Intersect(pubIdPerm2), PublisherIdentityPermission)
If Not (p3 Is Nothing) AndAlso Not (p3.Certificate Is Nothing) Then
Console.WriteLine(("The intersection of " & pubIdPerm1.Certificate.GetName() & " and " & pubIdPerm2.Certificate.GetName() & " = " & CType(p3, PublisherIdentityPermission).Certificate.GetName()))
Else
Console.WriteLine(("The intersection of " & pubIdPerm1.Certificate.GetName() & " and " & pubIdPerm2.Certificate.GetName() & " is null."))
End If
ContinueWhile2:
End While
ContinueWhile1:
End While
Return retCode
End Function 'IntersectDemo
'//Copy creates and returns an identical copy of the current permission.
Private Function CopyDemo() As Boolean
Dim retCode As Boolean = True
Dim publisher1 As [String]
Dim pubIdPerm1, pubIdPerm2 As PublisherIdentityPermission
Dim pubGen1 As New PublisherGenerator()
Dim pubGen2 As New PublisherGenerator()
pubGen1.ResetIndex()
While pubGen1.CreatePublisher(pubIdPerm1, publisher1)
If pubIdPerm1 Is Nothing Then
GoTo ContinueWhile1
End If
pubGen2.ResetIndex()
Try
pubIdPerm2 = CType(pubIdPerm1.Copy(), PublisherIdentityPermission)
If Not (pubIdPerm2 Is Nothing) Then
Console.WriteLine(("Result of copy = " & pubIdPerm2.ToString() & ControlChars.Lf))
Else
Console.WriteLine("Result of copy is null. " & ControlChars.Lf)
End If
Catch e As Exception
Console.WriteLine(("Copy failed :" & pubIdPerm1.ToString() & e.ToString()))
GoTo ContinueWhile1
End Try
ContinueWhile1:
End While
Return retCode
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 retCode As Boolean = True
Dim publisher1 As [String]
Dim pubIdPerm1, pubIdPerm2 As PublisherIdentityPermission
Dim pubGen1 As New PublisherGenerator()
Dim pubGen2 As New PublisherGenerator()
pubGen1.ResetIndex()
While pubGen1.CreatePublisher(pubIdPerm1, publisher1)
If pubIdPerm1 Is Nothing Then
GoTo ContinueWhile1
End If
pubGen2.ResetIndex()
Try
pubIdPerm2 = New PublisherIdentityPermission(PermissionState.None)
pubIdPerm2.FromXml(pubIdPerm1.ToXml())
Console.WriteLine(("Result of ToFromXml = " & pubIdPerm2.ToString() & ControlChars.Lf))
Catch e As Exception
Console.WriteLine(("ToFromXml failed :" & pubIdPerm1.ToString() & e.ToString()))
GoTo ContinueWhile1
End Try
ContinueWhile1:
End While
Return retCode
End Function 'ToFromXmlDemo
' Invoke all demos.
Public Function RunDemo() As Boolean
Dim ret As Boolean = True
Dim retTmp As Boolean
' Call the IsSubsetOf demo.
retTmp = IsSubsetOfDemo()
If retTmp Then
Console.Out.WriteLine("IsSubsetOf demo completed successfully.")
Else
Console.Out.WriteLine("IsSubsetOf demo failed.")
End If
ret = retTmp AndAlso ret
Console.WriteLine("************************************************************************")
' Call the 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
Console.WriteLine("************************************************************************")
' Call the 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
Console.WriteLine("************************************************************************")
' Call the 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
Console.WriteLine("************************************************************************")
' Call the 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
Console.WriteLine("************************************************************************")
Return ret
End Function 'RunDemo
' Test harness.
Public Overloads Shared Sub Main(ByVal args() As [String])
Try
Dim democase As New PublisherIdentityDemo()
Dim ret As Boolean = democase.RunDemo()
If ret Then
Console.Out.WriteLine("The PublisherIdentityPermission 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("The PublisherIdentityPermission 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("The PublisherIdentityPermission 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 'PublisherIdentityDemo
' This class generates PublisherIdentityPermission objects.
Friend Class PublisherGenerator
Private publisherCertificate(1) As X509Certificate
Private publisherIndex As Integer = 0
Public Sub New()
Try
Dim fs1 As New FileStream("MyCert1.cer", FileMode.Open)
Dim certSBytes1(Fix(fs1.Length)) As [Byte]
fs1.Read(certSBytes1, 0, Fix(fs1.Length))
publisherCertificate(0) = New X509Certificate(certSBytes1)
fs1.Close()
Dim fs2 As New FileStream("MyCert2.cer", FileMode.Open)
Dim certSBytes2(Fix(fs2.Length)) As [Byte]
fs2.Read(certSBytes2, 0, Fix(fs2.Length))
publisherCertificate(1) = New X509Certificate(certSBytes2)
fs2.Close()
ResetIndex()
Catch e As Exception
Console.WriteLine(("Cannot create certificate : " & e.ToString()))
Console.Out.WriteLine("PublisherIdentityPermission demo failed.")
System.Environment.ExitCode = 101
End Try
End Sub 'New
Public Sub ResetIndex()
publisherIndex = 0
End Sub 'ResetIndex
' CreatePublisher creates a PublisherIdentityPermission.
Public Function CreatePublisher(ByRef publisherPerm As PublisherIdentityPermission, ByRef publisher As [String]) As Boolean
If publisherIndex >= publisherCertificate.Length & 1 Then
publisherPerm = Nothing
publisher = "null"
Return False
End If
If publisherIndex >= publisherCertificate.Length Then
publisherPerm = New PublisherIdentityPermission(PermissionState.None)
publisher = "null"
publisherIndex &= 1
Return True
End If
Dim cert As X509Certificate = publisherCertificate(publisherIndex)
publisherIndex = publisherIndex + 1
publisher = cert.ToString()
Try
publisherPerm = New PublisherIdentityPermission(cert)
Return True
Catch e As Exception
Console.WriteLine(("Cannot create PublisherIdentityPermission: " & publisher & " " & e.ToString()))
publisherPerm = New PublisherIdentityPermission(PermissionState.None)
publisher = "null"
Return True
End Try
End Function 'CreatePublisher
End Class 'PublisherGenerator
' End of PublisherGenerator.
using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Cryptography.X509Certificates;
using System.IO;
[assembly: CLSCompliant(true)]
public class PublisherIdentityDemo
{
// IsSubsetOf determines whether the current permission is a subset of the specified permission.
private bool IsSubsetOfDemo()
{
bool retCode = true;
String publisher1, publisher2;
PublisherIdentityPermission pubIdPerm1, pubIdPerm2;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while (pubGen1.CreatePublisher(out pubIdPerm1, out publisher1))
{
if (pubIdPerm1 == null) continue;
pubGen2.ResetIndex();
while (pubGen2.CreatePublisher(out pubIdPerm2, out publisher2))
{
if (pubIdPerm2 == null) continue;
try
{
// Check for whether either of the publishers is null.
// An exception will be thrown if either is null.
if (publisher1 != "null" && publisher2 != "null")
{
if (pubIdPerm1.IsSubsetOf(pubIdPerm2))
{
Console.WriteLine(pubIdPerm1.Certificate.GetName() +
" is a subset of " + pubIdPerm2.Certificate.GetName() + "\n");
}
else
{
Console.WriteLine(pubIdPerm1.Certificate.GetName() +
" is not a subset of " + pubIdPerm2.Certificate.GetName() + "\n");
}
}
}
catch
{
Console.WriteLine("An exception was thrown for " + publisher1 + " is a subset of " +
publisher2 + "\n.");
retCode = false;
}
}
}
return retCode;
}
// Union creates a new permission that is the union of the current permission and the specified permission.
private bool UnionDemo()
{
bool retCode = true;
String publisher1, publisher2;
PublisherIdentityPermission pubIdPerm1, pubIdPerm2;
IPermission p3;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while (pubGen1.CreatePublisher(out pubIdPerm1, out publisher1))
{
if (pubIdPerm1 == null | pubIdPerm1.Certificate == null) continue;
pubGen2.ResetIndex();
while (pubGen2.CreatePublisher(out pubIdPerm2, out publisher2))
{
if (pubIdPerm2 == null | pubIdPerm2.Certificate == null) continue;
try
{
p3 = pubIdPerm1.Union(pubIdPerm2);
if (p3 != null)
{
Console.WriteLine("The union of " + pubIdPerm1.Certificate.GetName() + " and "
+ pubIdPerm2.Certificate.GetName() + " = "
+ ((PublisherIdentityPermission)p3).Certificate.GetName());
}
else
{
Console.WriteLine("The union of " + pubIdPerm1.Certificate.GetName() + " and "
+ pubIdPerm2.Certificate.GetName() + " = null.");
}
}
catch (Exception e)
{
// Expected exception.
Console.WriteLine("An exception was thrown for union :" + e.Message + "\n");
}
}
}
return retCode;
}
// Intersect creates and returns a new permission that is the intersection of the
// current permission and the permission specified.
private bool IntersectDemo()
{
bool retCode = true;
String publisher1, publisher2;
PublisherIdentityPermission pubIdPerm1, pubIdPerm2, p3;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while (pubGen1.CreatePublisher(out pubIdPerm1, out publisher1))
{
if (pubIdPerm1 == null | pubIdPerm1.Certificate == null) continue;
pubGen2.ResetIndex();
while (pubGen2.CreatePublisher(out pubIdPerm2, out publisher2))
{
if (pubIdPerm2 == null | pubIdPerm2.Certificate == null) continue;
p3 = (PublisherIdentityPermission)pubIdPerm1.Intersect(pubIdPerm2);
if (p3 != null && p3.Certificate != null)
{
Console.WriteLine("The intersection of " + pubIdPerm1.Certificate.GetName() + " and " +
pubIdPerm2.Certificate.GetName() + " = " +
((PublisherIdentityPermission)p3).Certificate.GetName());
}
else
{
Console.WriteLine("The intersection of " + pubIdPerm1.Certificate.GetName() + " and " +
pubIdPerm2.Certificate.GetName() + " is null.");
}
}
}
return retCode;
}
////Copy creates and returns an identical copy of the current permission.
private bool CopyDemo()
{
bool retCode = true;
String publisher1;
PublisherIdentityPermission pubIdPerm1, pubIdPerm2;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while (pubGen1.CreatePublisher(out pubIdPerm1, out publisher1))
{
if (pubIdPerm1 == null) continue;
pubGen2.ResetIndex();
try
{
pubIdPerm2 = (PublisherIdentityPermission)pubIdPerm1.Copy();
if (pubIdPerm2 != null)
{
Console.WriteLine("Result of copy = " + pubIdPerm2.ToString() + "\n");
}
else
{
Console.WriteLine("Result of copy is null. \n");
}
}
catch (Exception e)
{
Console.WriteLine("Copy failed :" + pubIdPerm1.ToString() + e);
continue;
}
}
return retCode;
}
// 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 retCode = true;
String publisher1;
PublisherIdentityPermission pubIdPerm1, pubIdPerm2;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while (pubGen1.CreatePublisher(out pubIdPerm1, out publisher1))
{
if (pubIdPerm1 == null) continue;
pubGen2.ResetIndex();
try
{
pubIdPerm2 = new PublisherIdentityPermission(PermissionState.None);
pubIdPerm2.FromXml(pubIdPerm1.ToXml());
Console.WriteLine("Result of ToFromXml = " + pubIdPerm2.ToString() + "\n");
}
catch (Exception e)
{
Console.WriteLine("ToFromXml failed :" + pubIdPerm1.ToString() + e);
continue;
}
}
return retCode;
}
// Invoke all demos.
public bool RunDemo()
{
bool ret = true;
bool retTmp;
// Call the IsSubsetOf demo.
if (retTmp = IsSubsetOfDemo()) Console.Out.WriteLine("IsSubsetOf demo completed successfully.");
else
Console.Out.WriteLine("IsSubsetOf demo failed.");
ret = retTmp && ret;
Console.WriteLine("************************************************************************");
// Call the Union demo.
if (retTmp = UnionDemo()) Console.Out.WriteLine("Union demo completed successfully.");
else
Console.Out.WriteLine("Union demo failed.");
ret = retTmp && ret;
Console.WriteLine("************************************************************************");
// Call the Intersect demo.
if (retTmp = IntersectDemo()) Console.Out.WriteLine("Intersect demo completed successfully.");
else
Console.Out.WriteLine("Intersect demo failed.");
ret = retTmp && ret;
Console.WriteLine("************************************************************************");
// Call the Copy demo.
if (retTmp = CopyDemo()) Console.Out.WriteLine("Copy demo completed successfully. ");
else
Console.Out.WriteLine("Copy demo failed.");
ret = retTmp && ret;
Console.WriteLine("************************************************************************");
// Call the ToFromXml demo
if (retTmp = ToFromXmlDemo()) Console.Out.WriteLine("ToFromXml demo completed successfully.");
else
Console.Out.WriteLine("ToFromXml demo failed.");
ret = retTmp && ret;
Console.WriteLine("************************************************************************");
return ret;
}
// Test harness.
public static void Main(String[] args)
{
try
{
PublisherIdentityDemo democase = new PublisherIdentityDemo();
bool ret = democase.RunDemo();
if (ret)
{
Console.Out.WriteLine("The PublisherIdentityPermission demo completed successfully.");
Console.Out.WriteLine("Press the Enter key to exit.");
string consoleInput = Console.ReadLine();
System.Environment.ExitCode = 100;
}
else
{
Console.Out.WriteLine("The PublisherIdentityPermission 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("The PublisherIdentityPermission 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 PublisherIdentityPermission objects.
internal class PublisherGenerator
{
private X509Certificate[] publisherCertificate = new X509Certificate[2];
private int publisherIndex = 0;
public PublisherGenerator()
{
try
{
FileStream fs1 = new FileStream("MyCert1.cer", FileMode.Open);
Byte[] certSBytes1 = new Byte[(int)fs1.Length];
fs1.Read(certSBytes1, 0, (int)fs1.Length);
publisherCertificate[0] = new X509Certificate(certSBytes1);
fs1.Close();
FileStream fs2 = new FileStream("MyCert2.cer", FileMode.Open);
Byte[] certSBytes2 = new Byte[(int)fs2.Length];
fs2.Read(certSBytes2, 0, (int)fs2.Length);
publisherCertificate[1] = new X509Certificate(certSBytes2);
fs2.Close();
ResetIndex();
}
catch (Exception e)
{
Console.WriteLine("Cannot create certificate : " + e);
Console.Out.WriteLine("PublisherIdentityPermission demo failed.");
System.Environment.ExitCode = 101;
}
}
public void ResetIndex()
{
publisherIndex = 0;
}
// CreatePublisher creates a PublisherIdentityPermission.
public bool CreatePublisher(out PublisherIdentityPermission publisherPerm, out String publisher)
{
if (publisherIndex >= publisherCertificate.Length + 1)
{
publisherPerm = null;
publisher = "null";
return false;
}
if (publisherIndex >= publisherCertificate.Length)
{
publisherPerm = new PublisherIdentityPermission(PermissionState.None);
publisher = "null";
publisherIndex++;
return true;
}
X509Certificate cert = publisherCertificate[publisherIndex++];
publisher = cert.ToString();
try
{
publisherPerm = new PublisherIdentityPermission(cert);
return true;
}
catch (Exception e)
{
Console.WriteLine("Cannot create PublisherIdentityPermission: " + publisher + " " + e);
publisherPerm = new PublisherIdentityPermission(PermissionState.None);
publisher = "null";
return true;
}
}
} // End of PublisherGenerator.
using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Cryptography::X509Certificates;
using namespace System::IO;
// This class generates PublisherIdentityPermission objects.
[assembly:CLSCompliant(true)];
private ref class PublisherGenerator
{
private:
array<X509Certificate^>^publisherCertificate;
int publisherIndex;
public:
PublisherGenerator()
{
publisherCertificate = gcnew array<X509Certificate^>(2);
try
{
FileStream^ fs1 = gcnew FileStream( "MyCert1.cer",FileMode::Open );
array<Byte>^certSBytes1 = gcnew array<Byte>((int)fs1->Length);
fs1->Read( certSBytes1, 0, (int)fs1->Length );
publisherCertificate[ 0 ] = gcnew X509Certificate( certSBytes1 );
fs1->Close();
FileStream^ fs2 = gcnew FileStream( "MyCert2.cer",FileMode::Open );
array<Byte>^certSBytes2 = gcnew array<Byte>((int)fs2->Length);
fs2->Read( certSBytes2, 0, (int)fs2->Length );
publisherCertificate[ 1 ] = gcnew X509Certificate( certSBytes2 );
fs2->Close();
ResetIndex();
}
catch ( Exception^ e )
{
Console::WriteLine( "Cannot create certificate : {0}", e );
Console::WriteLine( "PublisherIdentityPermission demo failed." );
System::Environment::ExitCode = 101;
}
}
void ResetIndex()
{
publisherIndex = 0;
}
// CreatePublisher creates a PublisherIdentityPermission.
bool CreatePublisher( [Out]interior_ptr<PublisherIdentityPermission^> publisherPerm, [Out]interior_ptr<String^> publisher )
{
if ( publisherIndex >= publisherCertificate->Length + 1 )
{
*publisherPerm = nullptr;
*publisher = "null";
return false;
}
if ( publisherIndex >= publisherCertificate->Length )
{
*publisherPerm = gcnew PublisherIdentityPermission( PermissionState::None );
*publisher = "null";
publisherIndex++;
return true;
}
X509Certificate^ cert = publisherCertificate[ publisherIndex++ ];
*publisher = cert->ToString();
try
{
*publisherPerm = gcnew PublisherIdentityPermission( cert );
return true;
}
catch ( Exception^ e )
{
Console::WriteLine( "Cannot create PublisherIdentityPermission: {0} {1}", *publisher, e );
*publisherPerm = gcnew PublisherIdentityPermission( PermissionState::None );
*publisher = "null";
return true;
}
}
};
// End of PublisherGenerator.
// IsSubsetOf determines whether the current permission is a subset of the specified permission.
bool IsSubsetOfDemo()
{
bool retCode = true;
String^ publisher1;
String^ publisher2;
PublisherIdentityPermission^ pubIdPerm1;
PublisherIdentityPermission^ pubIdPerm2;
PublisherGenerator^ pubGen1 = gcnew PublisherGenerator;
PublisherGenerator^ pubGen2 = gcnew PublisherGenerator;
pubGen1->ResetIndex();
while ( pubGen1->CreatePublisher( &pubIdPerm1, &publisher1 ) )
{
if ( pubIdPerm1 == nullptr )
continue;
pubGen2->ResetIndex();
while ( pubGen2->CreatePublisher( &pubIdPerm2, &publisher2 ) )
{
if ( pubIdPerm2 == nullptr )
continue;
try
{
// Check for whether either of the publishers is null.
// An exception will be thrown if either is null.
if ( !publisher1->Equals( "null" ) && !publisher2->Equals( "null" ) )
{
if ( pubIdPerm1->IsSubsetOf( pubIdPerm2 ) )
{
Console::WriteLine( "{0} is a subset of {1}\n", pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName() );
}
else
{
Console::WriteLine( "{0} is not a subset of {1}\n", pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName() );
}
}
}
catch ( Exception^ )
{
Console::WriteLine( "An exception was thrown for {0} is a subset of {1}\n.", publisher1, publisher2 );
retCode = false;
}
}
}
return retCode;
}
// Union creates a new permission that is the union of the current permission and the specified permission.
bool UnionDemo()
{
bool retCode = true;
String^ publisher1;
String^ publisher2;
PublisherIdentityPermission^ pubIdPerm1;
PublisherIdentityPermission^ pubIdPerm2;
IPermission^ p3;
PublisherGenerator^ pubGen1 = gcnew PublisherGenerator;
PublisherGenerator^ pubGen2 = gcnew PublisherGenerator;
pubGen1->ResetIndex();
while ( pubGen1->CreatePublisher( &pubIdPerm1, &publisher1 ) )
{
if ( pubIdPerm1 == nullptr || pubIdPerm1->Certificate == nullptr )
continue;
pubGen2->ResetIndex();
while ( pubGen2->CreatePublisher( &pubIdPerm2, &publisher2 ) )
{
if ( pubIdPerm2 == nullptr || pubIdPerm2->Certificate == nullptr )
continue;
p3 = pubIdPerm1->Union( pubIdPerm2 );
if ( p3 != nullptr )
{
Console::WriteLine( "The union of {0} and {1} = {2}", pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName(), (dynamic_cast<PublisherIdentityPermission^>(p3))->Certificate->GetName() );
}
else
{
Console::WriteLine( "The union of {0} and {1} = null.", pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName() );
}
}
}
return retCode;
}
// Intersect creates and returns a new permission that is the intersection of the
// current permission and the permission specified.
bool IntersectDemo()
{
bool retCode = true;
String^ publisher1;
String^ publisher2;
PublisherIdentityPermission^ pubIdPerm1;
PublisherIdentityPermission^ pubIdPerm2;
PublisherIdentityPermission^ p3;
PublisherGenerator^ pubGen1 = gcnew PublisherGenerator;
PublisherGenerator^ pubGen2 = gcnew PublisherGenerator;
pubGen1->ResetIndex();
while ( pubGen1->CreatePublisher( &pubIdPerm1, &publisher1 ) )
{
if ( pubIdPerm1 == nullptr || pubIdPerm1->Certificate == nullptr )
continue;
pubGen2->ResetIndex();
while ( pubGen2->CreatePublisher( &pubIdPerm2, &publisher2 ) )
{
if ( pubIdPerm2 == nullptr || pubIdPerm2->Certificate == nullptr )
continue;
p3 = dynamic_cast<PublisherIdentityPermission^>(pubIdPerm1->Intersect( pubIdPerm2 ));
if ( p3 != nullptr && p3->Certificate != nullptr )
{
Console::WriteLine( "The intersection of {0} and {1} = {2}", pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName(), (dynamic_cast<PublisherIdentityPermission^>(p3))->Certificate->GetName() );
}
else
{
Console::WriteLine( "The intersection of {0} and {1} is null.", pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName() );
}
}
}
return retCode;
}
////Copy creates and returns an identical copy of the current permission.
bool CopyDemo()
{
bool retCode = true;
String^ publisher1;
PublisherIdentityPermission^ pubIdPerm1;
PublisherIdentityPermission^ pubIdPerm2;
PublisherGenerator^ pubGen1 = gcnew PublisherGenerator;
PublisherGenerator^ pubGen2 = gcnew PublisherGenerator;
pubGen1->ResetIndex();
while ( pubGen1->CreatePublisher( &pubIdPerm1, &publisher1 ) )
{
if ( pubIdPerm1 == nullptr )
continue;
pubGen2->ResetIndex();
try
{
pubIdPerm2 = dynamic_cast<PublisherIdentityPermission^>(pubIdPerm1->Copy());
if ( pubIdPerm2 != nullptr )
{
Console::WriteLine( "Result of copy = {0}\n", pubIdPerm2 );
}
else
{
Console::WriteLine( "Result of copy is null. \n" );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Copy failed :{0}{1}", pubIdPerm1, e );
continue;
}
}
return retCode;
}
// ToXml creates an XML encoding of the permission and its current state; FromXml
// reconstructs a permission with the specified state from the XML encoding.
bool ToFromXmlDemo()
{
bool retCode = true;
String^ publisher1;
PublisherIdentityPermission^ pubIdPerm1;
PublisherIdentityPermission^ pubIdPerm2;
PublisherGenerator^ pubGen1 = gcnew PublisherGenerator;
PublisherGenerator^ pubGen2 = gcnew PublisherGenerator;
pubGen1->ResetIndex();
while ( pubGen1->CreatePublisher( &pubIdPerm1, &publisher1 ) )
{
if ( pubIdPerm1 == nullptr )
continue;
pubGen2->ResetIndex();
try
{
pubIdPerm2 = gcnew PublisherIdentityPermission( PermissionState::None );
pubIdPerm2->FromXml( pubIdPerm1->ToXml() );
Console::WriteLine( "Result of ToFromXml = {0}\n", pubIdPerm2 );
}
catch ( Exception^ e )
{
Console::WriteLine( "ToFromXml failed :{0}{1}", pubIdPerm1, e );
continue;
}
}
return retCode;
}
// Invoke all demos.
bool RunDemo()
{
bool ret = true;
bool retTmp;
// Call the IsSubsetOf demo.
if ( retTmp = IsSubsetOfDemo() )
Console::WriteLine( "IsSubsetOf demo completed successfully." );
else
Console::WriteLine( "IsSubsetOf demo failed." );
ret = retTmp && ret;
Console::WriteLine( "************************************************************************" );
// Call the Union demo.
if ( retTmp = UnionDemo() )
Console::WriteLine( "Union demo completed successfully." );
else
Console::WriteLine( "Union demo failed." );
ret = retTmp && ret;
Console::WriteLine( "************************************************************************" );
// Call the Intersect demo.
if ( retTmp = IntersectDemo() )
Console::WriteLine( "Intersect demo completed successfully." );
else
Console::WriteLine( "Intersect demo failed." );
ret = retTmp && ret;
Console::WriteLine( "************************************************************************" );
// Call the Copy demo.
if ( retTmp = CopyDemo() )
Console::WriteLine( "Copy demo completed successfully. " );
else
Console::WriteLine( "Copy demo failed." );
ret = retTmp && ret;
Console::WriteLine( "************************************************************************" );
// Call the ToFromXml demo
if ( retTmp = ToFromXmlDemo() )
Console::WriteLine( "ToFromXml demo completed successfully." );
else
Console::WriteLine( "ToFromXml demo failed." );
ret = retTmp && ret;
Console::WriteLine( "************************************************************************" );
return ret;
}
// Test harness.
int main()
{
try
{
bool ret = RunDemo();
if ( ret )
{
Console::WriteLine( "The PublisherIdentityPermission demo completed successfully." );
Console::WriteLine( "Press the Enter key to exit." );
Console::ReadLine();
System::Environment::ExitCode = 100;
}
else
{
Console::WriteLine( "The PublisherIdentityPermission demo failed." );
Console::WriteLine( "Press the Enter key to exit." );
Console::ReadLine();
System::Environment::ExitCode = 101;
}
}
catch ( Exception^ e )
{
Console::WriteLine( "The PublisherIdentityPermission demo failed." );
Console::WriteLine( e );
Console::WriteLine( "Press the Enter key to exit." );
Console::ReadLine();
System::Environment::ExitCode = 101;
}
}
import System.*;
import System.Security.*;
import System.Security.Permissions.*;
import System.Security.Cryptography.X509Certificates.*;
import System.IO.*;
/** @assembly CLSCompliant(true)
*/
public class PublisherIdentityDemo
{
// IsSubsetOf determines whether the current permission is a subset
// of the specified permission.
private boolean IsSubsetOfDemo()
{
boolean retCode = true;
String publisher1 = null;
String publisher2 = null;
PublisherIdentityPermission pubIdPerm1 = null;
PublisherIdentityPermission pubIdPerm2 = null;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while(pubGen1.CreatePublisher(pubIdPerm1, publisher1)) {
if (pubIdPerm1 == null) {
continue ;
}
pubGen2.ResetIndex();
while(pubGen2.CreatePublisher(pubIdPerm2, publisher2)) {
if (pubIdPerm2 == null) {
continue ;
}
try {
// Check for whether either of the publishers is null.
// An exception will be thrown if either is null.
if (publisher1 != "null" && publisher2 != "null") {
if ( pubIdPerm1.IsSubsetOf(pubIdPerm2) ) {
Console.WriteLine((pubIdPerm1.get_Certificate().
GetName() + " is a subset of "
+ pubIdPerm2.get_Certificate().GetName()
+ "\n"));
}
else {
Console.WriteLine((pubIdPerm1.get_Certificate().
GetName() + " is not a subset of "
+ pubIdPerm2.get_Certificate().GetName()
+ "\n"));
}
}
}
catch(System.Exception exp){
Console.WriteLine(("An exception was thrown for "
+ publisher1 + " is a subset of " + publisher2
+ "\n."));
retCode = false;
}
}
}
return retCode ;
} //IsSubsetOfDemo
// Union creates a new permission that is the union of the current
// permission and the specified permission.
private boolean UnionDemo()
{
boolean retCode = true;
String publisher1 = null;
String publisher2 = null;
PublisherIdentityPermission pubIdPerm1 = null;
PublisherIdentityPermission pubIdPerm2 = null;
IPermission p3;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while(pubGen1.CreatePublisher(pubIdPerm1, publisher1)) {
if (pubIdPerm1 == null | pubIdPerm1.get_Certificate()
== null) {
continue;
}
pubGen2.ResetIndex();
while(pubGen2.CreatePublisher(pubIdPerm2, publisher2)) {
if (pubIdPerm2 == null | pubIdPerm2.get_Certificate()
== null) {
continue;
}
p3 = pubIdPerm1.Union(pubIdPerm2);
if (p3 != null) {
Console.WriteLine(("The union of "
+ pubIdPerm1.get_Certificate().GetName() + " and "
+ pubIdPerm2.get_Certificate().GetName() + " = "
+ ((PublisherIdentityPermission)(p3)).get_Certificate().
GetName()));
}
else {
Console.WriteLine(("The union of "
+ pubIdPerm1.get_Certificate().GetName()
+ " and " + pubIdPerm2.get_Certificate().GetName()
+ " = null."));
}
}
}
return retCode ;
} //UnionDemo
// Intersect creates and returns a new permission that is the
// intersection of the current permission and the permission specified.
private boolean IntersectDemo()
{
boolean retCode = true;
String publisher1 = null;
String publisher2 = null;
PublisherIdentityPermission pubIdPerm1 = null;
PublisherIdentityPermission pubIdPerm2 = null;
PublisherIdentityPermission p3;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while(pubGen1.CreatePublisher(pubIdPerm1, publisher1)) {
if (pubIdPerm1 == null | pubIdPerm1.get_Certificate()
== null) {
continue;
}
pubGen2.ResetIndex();
while(pubGen2.CreatePublisher(pubIdPerm2, publisher2)) {
if (pubIdPerm2 == null | pubIdPerm2.get_Certificate()
== null) {
continue;
}
p3 =((PublisherIdentityPermission)(
pubIdPerm1.Intersect(pubIdPerm2)));
if (p3 != null && p3.get_Certificate() != null) {
Console.WriteLine(("The intersection of "
+ pubIdPerm1.get_Certificate().GetName()
+ " and " + pubIdPerm2.get_Certificate().GetName()
+ " = " + ((PublisherIdentityPermission)(p3)).
get_Certificate().GetName()));
}
else {
Console.WriteLine(("The intersection of "
+ pubIdPerm1.get_Certificate().GetName()
+ " and " + pubIdPerm2.get_Certificate().GetName()
+ " is null."));
}
}
}
return retCode ;
} //IntersectDemo
//Copy creates and returns an identical copy of the current permission.
private boolean CopyDemo()
{
boolean retCode = true;
String publisher1 = null;
PublisherIdentityPermission pubIdPerm1 = null;
PublisherIdentityPermission pubIdPerm2 = null;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while(pubGen1.CreatePublisher(pubIdPerm1, publisher1)) {
if (pubIdPerm1 == null) {
continue ;
}
pubGen2.ResetIndex();
try {
pubIdPerm2 =((PublisherIdentityPermission)(pubIdPerm1.Copy()));
if ( pubIdPerm2 != null ) {
Console.WriteLine(("Result of copy = " +
pubIdPerm2.ToString() + "\n"));
}
else {
Console.WriteLine("Result of copy is null. \n");
}
}
catch(System.Exception e) {
Console.WriteLine(("Copy failed :"
+ pubIdPerm1.ToString() + e));
continue ;
}
}
return retCode ;
} //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 retCode = true;
String publisher1 = null;
PublisherIdentityPermission pubIdPerm1 = null;
PublisherIdentityPermission pubIdPerm2 = null;
PublisherGenerator pubGen1 = new PublisherGenerator();
PublisherGenerator pubGen2 = new PublisherGenerator();
pubGen1.ResetIndex();
while(pubGen1.CreatePublisher(pubIdPerm1, publisher1)) {
if (pubIdPerm1 == null) {
continue ;
}
pubGen2.ResetIndex();
try {
pubIdPerm2 =
new PublisherIdentityPermission(PermissionState.None);
pubIdPerm2.FromXml(pubIdPerm1.ToXml());
Console.WriteLine(("Result of ToFromXml = "
+ pubIdPerm2.ToString() + "\n"));
}
catch(System.Exception e) {
Console.WriteLine(("ToFromXml failed :"
+ pubIdPerm1.ToString() + e));
continue ;
}
}
return retCode ;
} //ToFromXmlDemo
// Invoke all demos.
public boolean RunDemo()
{
boolean ret = true;
boolean retTmp;
// Call the IsSubsetOf demo.
if (retTmp = IsSubsetOfDemo()) {
Console.get_Out().WriteLine("IsSubsetOf demo "
+ "completed successfully.");
}
else {
Console.get_Out().WriteLine("IsSubsetOf demo failed.");
}
ret = retTmp && ret;
Console.WriteLine("******************************"
+ "******************************************");
// Call the Union demo.
if (retTmp = UnionDemo()) {
Console.get_Out().WriteLine("Union demo completed successfully.");
}
else {
Console.get_Out().WriteLine("Union demo failed.");
}
ret = retTmp && ret;
Console.WriteLine("************************************"
+ "************************************");
// Call the Intersect demo.
if (retTmp = IntersectDemo()) {
Console.get_Out().WriteLine("Intersect demo "
+ "completed successfully.");
}
else {
Console.get_Out().WriteLine("Intersect demo failed.");
}
ret = retTmp && ret;
Console.WriteLine("*****************************************"
+ "*******************************");
// Call the Copy demo.
if (retTmp = CopyDemo()) {
Console.get_Out().WriteLine("Copy demo completed successfully. ");
}
else {
Console.get_Out().WriteLine("Copy demo failed.");
}
ret = retTmp && ret;
Console.WriteLine("*********************************************"
+ "***************************");
// Call the ToFromXml demo
if (retTmp = ToFromXmlDemo()) {
Console.get_Out().WriteLine("ToFromXml demo "
+ "completed successfully.");
}
else {
Console.get_Out().WriteLine("ToFromXml demo failed.");
}
ret = retTmp && ret;
Console.WriteLine("**********************************"
+ "**************************************");
return ret ;
} //RunDemo
// Test harness.
public static void main(String[] args)
{
try {
PublisherIdentityDemo democase = new PublisherIdentityDemo();
boolean ret = democase.RunDemo();
if (ret) {
Console.get_Out().WriteLine("The "
+ "PublisherIdentityPermission demo completed "
+ "successfully.");
Console.get_Out().WriteLine("Press the Enter key to exit.");
String consoleInput = Console.ReadLine();
System.Environment.set_ExitCode(100);
}
else {
Console.get_Out().WriteLine("The "
+ "PublisherIdentityPermission demo failed.");
Console.get_Out().WriteLine("Press the Enter key to exit.");
String consoleInput = Console.ReadLine();
System.Environment.set_ExitCode(101);
}
}
catch(System.Exception e) {
Console.get_Out().WriteLine("The "
+ "PublisherIdentityPermission demo failed.");
Console.WriteLine(e.ToString());
Console.get_Out().WriteLine("Press the Enter key to exit.");
String consoleInput = Console.ReadLine();
System.Environment.set_ExitCode(101);
}
}
} //PublisherIdentityDemo
// This class generates PublisherIdentityPermission objects.
class PublisherGenerator
{
private X509Certificate publisherCertificate[] = new X509Certificate[2];
private int publisherIndex = 0;
public PublisherGenerator()
{
try {
FileStream fs1 = new FileStream("MyCert1.cer", FileMode.Open);
System.Byte certSBytes1[] =
new System.Byte[(int)(fs1.get_Length())];
fs1.Read((ubyte[])certSBytes1, 0, (int)(fs1.get_Length()));
publisherCertificate .set_Item( 0 ,
new X509Certificate((ubyte[])certSBytes1) );
fs1.Close();
FileStream fs2 = new FileStream("MyCert2.cer", FileMode.Open);
System.Byte certSBytes2[] =
new System.Byte[(int)(fs2.get_Length())];
fs2.Read((ubyte[])certSBytes2, 0, (int)(fs2.get_Length()));
publisherCertificate .set_Item( 1 ,
new X509Certificate((ubyte[])certSBytes2) );
fs2.Close();
ResetIndex();
}
catch(System.Exception e) {
Console.WriteLine(("Cannot create certificate : " + e));
Console.get_Out().WriteLine("PublisherIdentityPermission "
+ "demo failed.");
System.Environment.set_ExitCode(101);
}
} //PublisherGenerator
public void ResetIndex()
{
publisherIndex = 0;
} //ResetIndex
// CreatePublisher creates a PublisherIdentityPermission.
public boolean CreatePublisher(
PublisherIdentityPermission publisherPerm, String publisher)
{
if (publisherIndex >= publisherCertificate.length + 1) {
publisherPerm = null;
publisher = "null";
return false ;
}
if (publisherIndex >= publisherCertificate.length) {
publisherPerm =
new PublisherIdentityPermission(PermissionState.None);
publisher = "null";
publisherIndex++;
return true ;
}
X509Certificate cert = publisherCertificate[publisherIndex++];
publisher = cert.ToString();
try {
publisherPerm = new PublisherIdentityPermission(cert);
return true ;
}
catch(System.Exception e) {
Console.WriteLine(("Cannot create PublisherIdentityPermission: "
+ publisher + " " + e));
publisherPerm =
new PublisherIdentityPermission(PermissionState.None);
publisher = "null";
return true ;
}
} //CreatePublisher
} //PublisherGenerator
상속 계층 구조
System.Object
System.Security.CodeAccessPermission
System.Security.Permissions.PublisherIdentityPermission
스레드로부터의 안전성
이 형식의 모든 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에서 지원
참고 항목
참조
PublisherIdentityPermission 멤버
System.Security.Permissions 네임스페이스
PublisherIdentityPermissionAttribute
Publisher
PublisherMembershipCondition