This documentation is archived and is not being maintained.

PublisherIdentityPermission Class

Represents the identity of a software publisher. This class cannot be inherited.

For a list of all members of this type, see PublisherIdentityPermission Members.

System.Object
   System.Security.CodeAccessPermission
      System.Security.Permissions.PublisherIdentityPermission

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Example

[Visual Basic] 
' This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
' of The PublisherIdentityPermission class.
' To execute this sample you will need two certification files, MyCert1.cer and MyCert2.cer.
' The certification files can be created using the Certification Creation Tool, MakeCert.exe.
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Cryptography.X509Certificates
Imports System.IO
Imports Microsoft.VisualBasic

<Assembly: CLSCompliant(True)> 

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()
            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
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.

[C#] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
// of The PublisherIdentityPermission class.
// To execute this sample you will need two certification files, MyCert1.cer and MyCert2.cer.
// The certification files can be created using the Certification Creation Tool, MakeCert.exe.

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;
                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.");
                }

            }
        }


        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.


[C++] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
// of The PublisherIdentityPermission class.
// To execute this sample you will need two certification files, MyCert1.cer and MyCert2.cer.
// The certification files can be created using the Certification Creation Tool, MakeCert.exe.

#using <mscorlib.dll>
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; 

[assembly:CLSCompliant(true)];

// This class generates PublisherIdentityPermission objects.

private __gc class PublisherGenerator
{
private:
   X509Certificate* publisherCertificate[]; 
   int publisherIndex;

public:
   PublisherGenerator()
   {
      publisherCertificate = new X509Certificate*[2]; 

      try
      {
         FileStream* fs1 = new FileStream(S"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(S"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(S"Cannot create certificate : {0}", e);
         Console::WriteLine(S"PublisherIdentityPermission demo failed.");
         System::Environment::ExitCode = 101;

      }
   }

   void ResetIndex(){

      publisherIndex = 0;

   }
   // CreatePublisher creates a PublisherIdentityPermission.
public:
   bool CreatePublisher([Out]PublisherIdentityPermission** publisherPerm, [Out]String** publisher)
   {

      if(publisherIndex >= publisherCertificate->Length + 1) 
      {
         *publisherPerm = 0;
         *publisher = S"null";
         return false;
      }

      if(publisherIndex >= publisherCertificate->Length) 
      {
         *publisherPerm = new PublisherIdentityPermission(PermissionState::None);
         *publisher=S"null";
         publisherIndex++;
         return true;

      }

      X509Certificate* cert = publisherCertificate[publisherIndex++];
      *publisher = cert->ToString();
      try
      {
         *publisherPerm = new PublisherIdentityPermission(cert);
         return true;
      } 
      catch(Exception* e)
      {
         Console::WriteLine(S"Cannot create PublisherIdentityPermission: {0} {1}", *publisher, e);
         *publisherPerm = new PublisherIdentityPermission(PermissionState::None);
         *publisher=S"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 = new PublisherGenerator();
   PublisherGenerator* pubGen2 = new PublisherGenerator();

   pubGen1->ResetIndex();
   while(pubGen1->CreatePublisher(&pubIdPerm1, &publisher1)) 
   {
      if(pubIdPerm1 == 0) continue;
      pubGen2->ResetIndex();
      while(pubGen2->CreatePublisher(&pubIdPerm2, &publisher2)) 
      {
         if(pubIdPerm2 == 0) continue;

         try
         {
            // Check for whether either of the publishers is null.
            // An exception will be thrown if either is null.
            if (!publisher1->Equals(S"null") && !publisher2->Equals(S"null"))
            {
               if (pubIdPerm1->IsSubsetOf(pubIdPerm2))
               {
                  Console::WriteLine(S"{0} is a subset of {1}\n",
                     pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName());
               }
               else
               {
                  Console::WriteLine(S"{0} is not a subset of {1}\n",
                     pubIdPerm1->Certificate->GetName(), pubIdPerm2->Certificate->GetName());
               }
            }

         }
         catch (Exception*)
         {
            Console::WriteLine(S"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 = new PublisherGenerator();
   PublisherGenerator* pubGen2 = new PublisherGenerator();

   pubGen1->ResetIndex();
   while(pubGen1->CreatePublisher(&pubIdPerm1, &publisher1)) 
   {
      if(pubIdPerm1 == 0 || pubIdPerm1->Certificate == 0) continue;
      pubGen2->ResetIndex();
      while(pubGen2->CreatePublisher(&pubIdPerm2, &publisher2)) 
      {
         if(pubIdPerm2 == 0 || pubIdPerm2->Certificate == 0) continue;
         p3 = pubIdPerm1->Union(pubIdPerm2);

         if(p3 != 0)
         {
            Console::WriteLine(S"The union of {0} and {1} = {2}",
               pubIdPerm1->Certificate->GetName(),
               pubIdPerm2->Certificate->GetName(),
               (dynamic_cast<PublisherIdentityPermission*>(p3))->Certificate->GetName());
         }
         else
         {
            Console::WriteLine (S"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 = new PublisherGenerator();
   PublisherGenerator* pubGen2 = new PublisherGenerator();

   pubGen1->ResetIndex();
   while(pubGen1->CreatePublisher(&pubIdPerm1, &publisher1)) 
   {
      if(pubIdPerm1 == 0 || pubIdPerm1->Certificate == 0) continue;
      pubGen2->ResetIndex();
      while(pubGen2->CreatePublisher(&pubIdPerm2, &publisher2)) 
      {
         if(pubIdPerm2 == 0 || pubIdPerm2->Certificate == 0) continue;
         p3 = dynamic_cast<PublisherIdentityPermission*>(pubIdPerm1->Intersect(pubIdPerm2));
         if (p3 != 0 && p3->Certificate != 0)
         {

            Console::WriteLine(S"The intersection of {0} and  {1} = {2}",
               pubIdPerm1->Certificate->GetName(),
               pubIdPerm2->Certificate->GetName(), 
               (dynamic_cast<PublisherIdentityPermission*>(p3))->Certificate->GetName());
         }
         else
         {
            Console::WriteLine(S"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 = new PublisherGenerator();
   PublisherGenerator* pubGen2 = new PublisherGenerator();

   pubGen1->ResetIndex();
   while(pubGen1->CreatePublisher(&pubIdPerm1, &publisher1)) 
   {
      if(pubIdPerm1 == 0) continue;
      pubGen2->ResetIndex();
      try
      {            
         pubIdPerm2 = dynamic_cast<PublisherIdentityPermission*>(pubIdPerm1->Copy());
         if (pubIdPerm2 != 0)
         {
            Console::WriteLine(S"Result of copy = {0}\n", pubIdPerm2);
         }
         else
         {
            Console::WriteLine(S"Result of copy is null. \n");
         }


      }
      catch(Exception* e) {
         Console::WriteLine(S"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 = new PublisherGenerator();
   PublisherGenerator* pubGen2 = new PublisherGenerator();

   pubGen1->ResetIndex();
   while(pubGen1->CreatePublisher(&pubIdPerm1, &publisher1)) 
   {
      if(pubIdPerm1 == 0) continue;
      pubGen2->ResetIndex();
      try
      {            
         pubIdPerm2= new PublisherIdentityPermission(PermissionState::None);
         pubIdPerm2->FromXml(pubIdPerm1->ToXml());
         Console::WriteLine(S"Result of ToFromXml = {0}\n", pubIdPerm2);
      }
      catch(Exception* e) 
      {
         Console::WriteLine(S"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(S"IsSubsetOf demo completed successfully.");
   else Console::WriteLine(S"IsSubsetOf demo failed.");
   ret=retTmp && ret;
   Console::WriteLine(S"************************************************************************");

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

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


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

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

   return ret;
}
// Test harness.
int main() 
{
   try
   {
      bool ret = RunDemo();
      if (ret)
      {
         Console::WriteLine(S"The PublisherIdentityPermission demo completed successfully.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 100;
      }
      else
      {
         Console::WriteLine(S"The PublisherIdentityPermission demo failed.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 101;
      }
   }
   catch(Exception* e)
   {
      Console::WriteLine(S"The PublisherIdentityPermission demo failed.");
      Console::WriteLine(e);
      Console::WriteLine(S"Press the Enter key to exit.");
      Console::ReadLine();
      System::Environment::ExitCode = 101;
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Security.Permissions

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

PublisherIdentityPermission Members | System.Security.Permissions Namespace | Permissions | Requesting Permissions | PublisherIdentityPermissionAttribute | Publisher | PublisherMembershipCondition

Show: