PublisherIdentityPermission Constructor (X509Certificate)

Initializes a new instance of the PublisherIdentityPermission class with the specified Authenticode X.509v3 certificate.

Namespace: System.Security.Permissions
Assembly: mscorlib (in mscorlib.dll)

public PublisherIdentityPermission (
	X509Certificate certificate
)
public PublisherIdentityPermission (
	X509Certificate certificate
)
public function PublisherIdentityPermission (
	certificate : X509Certificate
)

Parameters

certificate

An X.509 certificate representing the software publisher's identity.

Exception typeCondition

ArgumentNullException

The certificate parameter is a null reference (Nothing in Visual Basic).

ArgumentException

The certificate parameter is not a valid certificate.

The X.509 certificate defines the identity of the specified software publisher, as established by Authenticode code signing.

The following code example shows the use of the PublisherIdentityPermission constructor. This code example is part of a larger example which can be found below.

string certPath = ("Publisher.cer");

// Create evidence for the Publisher.cer certificate.
Publisher publisher =
    new Publisher(X509Certificate.CreateFromCertFile(certPath));
Evidence evidence = new Evidence();
evidence.AddHost(publisher);

// Create an identity permission based on publisher evidence.
PublisherIdentityPermission x509Permission =
    (PublisherIdentityPermission)publisher.
    CreateIdentityPermission(evidence);

// Verify that callers higher in the stack have been granted 
// permission.
x509Permission.Demand();

String certPath = "Publisher.cer";
// Create evidence for the Publisher.cer certificate.
Publisher publisher = new Publisher(X509Certificate.
    CreateFromCertFile(certPath));
Evidence evidence = new Evidence();
evidence.AddHost(publisher);
// Create an identity permission based on publisher evidence.
PublisherIdentityPermission x509Permission 
    = (PublisherIdentityPermission)(
    publisher.CreateIdentityPermission(evidence));
// Verify that callers higher in the stack have been granted 
// permission.
x509Permission.Demand();

The following is the full code example which demonstrates both the declarative and imperative forms of the PublisherIdentityPermission class to demand an assembly is signed with a specific certificate.

using System;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Security.Cryptography.X509Certificates;

// Declarative security requesting evidence of the Publisher.cer 
// X509 certificate.
[assembly:PublisherIdentityPermissionAttribute(SecurityAction.RequestMinimum,
    CertFile="Publisher.cer")]
class PublisherIdentity
{
    [STAThread]
    static void Main(string[] args)
    {
        if (new PublisherIdentity().DemandCertificate())
        {
            Console.WriteLine("PublisherIdentity successfully verified " + 
                "the certificate.");
        }
        else
        {
            Console.WriteLine("PublisherIdentity was unable to verify the " +
                "certificate.");
        }
    }

    private bool DemandCertificate()
    {
        bool blRetVal = false;
        try 
        {
            string certPath = ("Publisher.cer");

            // Create evidence for the Publisher.cer certificate.
            Publisher publisher =
                new Publisher(X509Certificate.CreateFromCertFile(certPath));
            Evidence evidence = new Evidence();
            evidence.AddHost(publisher);

            // Create an identity permission based on publisher evidence.
            PublisherIdentityPermission x509Permission =
                (PublisherIdentityPermission)publisher.
                CreateIdentityPermission(evidence);

            // Verify that callers higher in the stack have been granted 
            // permission.
            x509Permission.Demand();

            blRetVal = true;
        }
        catch (SecurityException ex)
        {
            Console.WriteLine("An exception was thrown:" + ex.ToString());
        }

        return blRetVal;
    }
}
//
// This sample produces the following output:
//
// An exception was thrown:System.Security.SecurityException: Request for the
// permission of type System.Security.Permissions.PublisherIdentityPermission,
// mscorlib, Version=1.0.5000.0, Culture=neutral, 
// PublicKeyToken=b77a5c561934e089 failed.
//  at System.Security.CodeAccessSecurityEngine.CheckHelper(PermissionSet 
// grantedSet, PermissionSet deniedSet, CodeAccessPermission demand,
// PermissionToken permToken)
//  at System.Security.CodeAccessSecurityEngine.Check(PermissionToken
// permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32
// checkFrames, Int32 unrestrictedOverride)
//  at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission 
// cap, StackCrawlMark& stackMark)
//  at System.Security.CodeAccessPermission.Demand()
//  at WindowsApplication1.PublisherIdentity.DemandCertificate()

// The state of the failed permission was: 
// <IPermission class="System.Security.Permissions.PublisherIdentityPermission
// ,mscorlib, Version=1.0.5000.0, Culture=neutral, 
//  PublicKeyToken=b77a5c561934e089"
//            version="1"
//            X509v3Certificate="3082020E30820177A0030201020210491BB8DBE6EBC99
// 14A5ACF0698CC0E18300D06092A864886F70D0101040500301C311A30180603550403131150
// 75626C69736865724964656E74697479301E170D3034303232343031303233365A170D33393
// 13233313233353935395A301C311A3018060355040313115075626C69736865724964656E74
// 69747930819F300D06092A864886F70D010101050003818D0030818902818100BF589FB7CD2
// D6DB89EEF6B394A4012B7727AD71EC1E342234DF15391296B5A5667F90E4B36D0C39AE44186
// 12300F8DC1435E6390468DC055D5759D1C772D9221188B6EC93AE59783F33644B5FA9952D1F
// 079BA4E6AF253E3EA1505E10DFD1ABEEF74D9D216E690C9CF3A3AF01138B7D69AD0E7155D56
// 630A34A03014FEFBC7750203010001A351304F304D0603551D01044630448010BFB7A1D6CD5
// 289C1A2062A691D4E325EA11E301C311A3018060355040313115075626C6973686572496465
// 6E746974798210491BB8DBE6EBC9914A5ACF0698CC0E18300D06092A864886F70D010104050
// 00381810061476417D94228CAA1003D3A68BF4265DB607A5806BBB48A31AD7726DC73F7BB25
// 04171C5C11BC6903C5D649A05C27640007C7CCF0C32D66E7BB640E353F86CBAC2E74777B379
// 4222F82333933A80F9269EB4AB2319EA519F7F78922066F2C7A47F99286C8B898A5145F0C05
// 6FB82FBD1363472CD8C38E9225FD5CB7CD4520A8"/>
//
// PublisherIdentity was unable to verify the certificate.
//
// This sample completed successfully; press Exit to continue.

import System.*;
import System.Security.*;
import System.Security.Policy.*;
import System.Security.Permissions.*;
import System.Security.Cryptography.X509Certificates.*;

// Declarative security requesting evidence of the Publisher.cer 
// X509 certificate.
/** @assembly PublisherIdentityPermissionAttribute(SecurityAction.
    RequestMinimum, CertFile = "Publisher.cer")
 */
class PublisherIdentity
{

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        if ((new PublisherIdentity()).DemandCertificate()) {
            Console.WriteLine("PublisherIdentity successfully verified " 
                + "the certificate.");
        }
        else {
            Console.WriteLine("PublisherIdentity was unable to verify the " 
                + "certificate.");
        }
    } //main

    private boolean DemandCertificate()
    {
        boolean blRetVal = false;
        try {
            String certPath = "Publisher.cer";
            // Create evidence for the Publisher.cer certificate.
            Publisher publisher = new Publisher(X509Certificate.
                CreateFromCertFile(certPath));
            Evidence evidence = new Evidence();
            evidence.AddHost(publisher);
            // Create an identity permission based on publisher evidence.
            PublisherIdentityPermission x509Permission 
                = (PublisherIdentityPermission)(
                publisher.CreateIdentityPermission(evidence));
            // Verify that callers higher in the stack have been granted 
            // permission.
            x509Permission.Demand();
            blRetVal = true;
        }
        catch (SecurityException ex) {
            Console.WriteLine("An exception was thrown:" + ex.ToString());
        }
        return blRetVal;
    } //DemandCertificate
} //PublisherIdentity
//
// This sample produces the following output:
//
// An exception was thrown:System.Security.SecurityException: Request for the
// permission of type System.Security.Permissions.PublisherIdentityPermission,
// mscorlib, Version=1.0.5000.0, Culture=neutral, 
// PublicKeyToken=b77a5c561934e089 failed.
// at System.Security.CodeAccessSecurityEngine.CheckHelper(PermissionSet 
// grantedSet, PermissionSet deniedSet, CodeAccessPermission demand,
// PermissionToken permToken)
// at System.Security.CodeAccessSecurityEngine.Check(PermissionToken
// permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32
// checkFrames, Int32 unrestrictedOverride)
// at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission 
// cap, StackCrawlMark& stackMark)
// at System.Security.CodeAccessPermission.Demand()
// at WindowsApplication1.PublisherIdentity.DemandCertificate()
// The state of the failed permission was: 
// <IPermission class="System.Security.Permissions.PublisherIdentityPermission
// ,mscorlib, Version=1.0.5000.0, Culture=neutral, 
// PublicKeyToken=b77a5c561934e089"
// version="1"
// X509v3Certificate="3082020E30820177A0030201020210491BB8DBE6EBC99
// 14A5ACF0698CC0E18300D06092A864886F70D0101040500301C311A30180603550403131150
// 75626C69736865724964656E74697479301E170D3034303232343031303233365A170D33393
// 13233313233353935395A301C311A3018060355040313115075626C69736865724964656E74
// 69747930819F300D06092A864886F70D010101050003818D0030818902818100BF589FB7CD2
// D6DB89EEF6B394A4012B7727AD71EC1E342234DF15391296B5A5667F90E4B36D0C39AE44186
// 12300F8DC1435E6390468DC055D5759D1C772D9221188B6EC93AE59783F33644B5FA9952D1F
// 079BA4E6AF253E3EA1505E10DFD1ABEEF74D9D216E690C9CF3A3AF01138B7D69AD0E7155D56
// 630A34A03014FEFBC7750203010001A351304F304D0603551D01044630448010BFB7A1D6CD5
// 289C1A2062A691D4E325EA11E301C311A3018060355040313115075626C6973686572496465
// 6E746974798210491BB8DBE6EBC9914A5ACF0698CC0E18300D06092A864886F70D010104050
// 00381810061476417D94228CAA1003D3A68BF4265DB607A5806BBB48A31AD7726DC73F7BB25
// 04171C5C11BC6903C5D649A05C27640007C7CCF0C32D66E7BB640E353F86CBAC2E74777B379
// 4222F82333933A80F9269EB4AB2319EA519F7F78922066F2C7A47F99286C8B898A5145F0C05
// 6FB82FBD1363472CD8C38E9225FD5CB7CD4520A8"/>
//
// PublisherIdentity was unable to verify the certificate.
//
// This sample completed successfully; press Exit to continue.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show: