PackageDigitalSignatureManager.Sign Método

Definição

Assina uma lista de partes do pacote com um certificado X.509 especificado.

Sobrecargas

Sign(IEnumerable<Uri>)

Solicita ao usuário um certificado X.509, que é usado para assinar digitalmente uma lista especificada de partes do pacote.

Sign(IEnumerable<Uri>, X509Certificate)

Assina uma lista de partes do pacote com um determinado certificado X.509.

Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>)

Assina uma lista de partes e relações do pacote com um determinado certificado X.509.

Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>, String)

Assina uma lista de partes do pacote e relações de pacote com um determinado certificado X.509 e identificação (ID).

Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>, String, IEnumerable<DataObject>, IEnumerable<Reference>)

Assina uma lista de partes do pacote, relações de pacote ou objetos personalizados com um certificado X.509 especificado e uma ID (identificador de assinatura).

Exemplos

O exemplo a seguir mostra as etapas para assinar digitalmente uma lista de partes em um Package. Para obter o exemplo completo, consulte Criando um pacote com um exemplo de assinatura digital.

private static void SignAllParts(Package package)
{
    if (package == null)
        throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager
    PackageDigitalSignatureManager dsm =
        new PackageDigitalSignatureManager(package);
    dsm.CertificateOption =
        CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign
    // (GetParts() also includes PackageRelationship parts).
    System.Collections.Generic.List<Uri> toSign =
        new System.Collections.Generic.List<Uri>();
    foreach (PackagePart packagePart in package.GetParts())
    {
        // Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri);
    }

    // Add the URI for SignatureOrigin PackageRelationship part.
    // The SignatureOrigin relationship is created when Sign() is called.
    // Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));

    // Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin);

    // Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

    // Sign() will prompt the user to select a Certificate to sign with.
    try
    {
        dsm.Sign(toSign);
    }

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
    {
        MessageBox.Show(
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",
            MessageBoxButton.OK,
            MessageBoxImage.Exclamation);
    }
}// end:SignAllParts()
Private Shared Sub SignAllParts(ByVal package As Package)
    If package Is Nothing Then
        Throw New ArgumentNullException("SignAllParts(package)")
    End If

    ' Create the DigitalSignature Manager
    Dim dsm As New PackageDigitalSignatureManager(package)
    dsm.CertificateOption = CertificateEmbeddingOption.InSignaturePart

    ' Create a list of all the part URIs in the package to sign
    ' (GetParts() also includes PackageRelationship parts).
    Dim toSign As New System.Collections.Generic.List(Of Uri)()
    For Each packagePart As PackagePart In package.GetParts()
        ' Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri)
    Next

    ' Add the URI for SignatureOrigin PackageRelationship part.
    ' The SignatureOrigin relationship is created when Sign() is called.
    ' Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin))

    ' Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin)

    ' Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(New Uri("/", UriKind.RelativeOrAbsolute)))

    ' Sign() will prompt the user to select a Certificate to sign with.
    Try
        dsm.Sign(toSign)
    Catch ex As CryptographicException

        ' If there are no certificates or the SmartCard manager is
        ' not running, catch the exception and show an error message.
        MessageBox.Show("Cannot Sign" & vbLf & ex.Message, "No Digital Certificates Available", MessageBoxButton.OK, MessageBoxImage.Exclamation)

    End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
' end:SignAllParts()

Sign(IEnumerable<Uri>)

Solicita ao usuário um certificado X.509, que é usado para assinar digitalmente uma lista especificada de partes do pacote.

public:
 System::IO::Packaging::PackageDigitalSignature ^ Sign(System::Collections::Generic::IEnumerable<Uri ^> ^ parts);
public System.IO.Packaging.PackageDigitalSignature Sign (System.Collections.Generic.IEnumerable<Uri> parts);
member this.Sign : seq<Uri> -> System.IO.Packaging.PackageDigitalSignature
Public Function Sign (parts As IEnumerable(Of Uri)) As PackageDigitalSignature

Parâmetros

parts
IEnumerable<Uri>

A lista de URIs (Uniform Resource Identifiers) para os elementos PackagePart a serem assinados.

Retornos

A assinatura digital usada para assinar a lista de parts.

Exemplos

O exemplo a seguir mostra como assinar digitalmente uma lista de partes do pacote. Para obter o exemplo completo, consulte Criando um pacote com um exemplo de assinatura digital.

private static void SignAllParts(Package package)
{
    if (package == null)
        throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager
    PackageDigitalSignatureManager dsm =
        new PackageDigitalSignatureManager(package);
    dsm.CertificateOption =
        CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign
    // (GetParts() also includes PackageRelationship parts).
    System.Collections.Generic.List<Uri> toSign =
        new System.Collections.Generic.List<Uri>();
    foreach (PackagePart packagePart in package.GetParts())
    {
        // Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri);
    }

    // Add the URI for SignatureOrigin PackageRelationship part.
    // The SignatureOrigin relationship is created when Sign() is called.
    // Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));

    // Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin);

    // Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

    // Sign() will prompt the user to select a Certificate to sign with.
    try
    {
        dsm.Sign(toSign);
    }

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
    {
        MessageBox.Show(
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",
            MessageBoxButton.OK,
            MessageBoxImage.Exclamation);
    }
}// end:SignAllParts()
Private Shared Sub SignAllParts(ByVal package As Package)
    If package Is Nothing Then
        Throw New ArgumentNullException("SignAllParts(package)")
    End If

    ' Create the DigitalSignature Manager
    Dim dsm As New PackageDigitalSignatureManager(package)
    dsm.CertificateOption = CertificateEmbeddingOption.InSignaturePart

    ' Create a list of all the part URIs in the package to sign
    ' (GetParts() also includes PackageRelationship parts).
    Dim toSign As New System.Collections.Generic.List(Of Uri)()
    For Each packagePart As PackagePart In package.GetParts()
        ' Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri)
    Next

    ' Add the URI for SignatureOrigin PackageRelationship part.
    ' The SignatureOrigin relationship is created when Sign() is called.
    ' Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin))

    ' Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin)

    ' Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(New Uri("/", UriKind.RelativeOrAbsolute)))

    ' Sign() will prompt the user to select a Certificate to sign with.
    Try
        dsm.Sign(toSign)
    Catch ex As CryptographicException

        ' If there are no certificates or the SmartCard manager is
        ' not running, catch the exception and show an error message.
        MessageBox.Show("Cannot Sign" & vbLf & ex.Message, "No Digital Certificates Available", MessageBoxButton.OK, MessageBoxImage.Exclamation)

    End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
' end:SignAllParts()

Comentários

Para tornar a caixa de diálogo de seleção de certificado modal para uma janela específica, defina a ParentWindow propriedade antes de chamar Sign.

Sign não solicitará certificados se não houver nenhum no repositório de certificados padrão.

Aplica-se a

Sign(IEnumerable<Uri>, X509Certificate)

Assina uma lista de partes do pacote com um determinado certificado X.509.

public:
 System::IO::Packaging::PackageDigitalSignature ^ Sign(System::Collections::Generic::IEnumerable<Uri ^> ^ parts, System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate);
public System.IO.Packaging.PackageDigitalSignature Sign (System.Collections.Generic.IEnumerable<Uri> parts, System.Security.Cryptography.X509Certificates.X509Certificate certificate);
member this.Sign : seq<Uri> * System.Security.Cryptography.X509Certificates.X509Certificate -> System.IO.Packaging.PackageDigitalSignature
Public Function Sign (parts As IEnumerable(Of Uri), certificate As X509Certificate) As PackageDigitalSignature

Parâmetros

parts
IEnumerable<Uri>

A lista de URIs (Uniform Resource Identifiers) para os elementos PackagePart a serem assinados.

certificate
X509Certificate

O certificado X.509 a ser usado para assinar digitalmente cada uma das parts especificadas.

Retornos

A assinatura digital usada para assinar a lista de parts especificada ou null se não for possível encontrar nenhum certificado ou se o usuário tiver clicado em “Cancelar” na caixa de diálogo de seleção de certificado.

Exemplos

O exemplo a seguir mostra como assinar digitalmente uma lista de partes em um Package. Para obter o exemplo completo, consulte Criando um pacote com um exemplo de assinatura digital.

private static void SignAllParts(Package package)
{
    if (package == null)
        throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager
    PackageDigitalSignatureManager dsm =
        new PackageDigitalSignatureManager(package);
    dsm.CertificateOption =
        CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign
    // (GetParts() also includes PackageRelationship parts).
    System.Collections.Generic.List<Uri> toSign =
        new System.Collections.Generic.List<Uri>();
    foreach (PackagePart packagePart in package.GetParts())
    {
        // Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri);
    }

    // Add the URI for SignatureOrigin PackageRelationship part.
    // The SignatureOrigin relationship is created when Sign() is called.
    // Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));

    // Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin);

    // Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

    // Sign() will prompt the user to select a Certificate to sign with.
    try
    {
        dsm.Sign(toSign);
    }

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
    {
        MessageBox.Show(
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",
            MessageBoxButton.OK,
            MessageBoxImage.Exclamation);
    }
}// end:SignAllParts()
Private Shared Sub SignAllParts(ByVal package As Package)
    If package Is Nothing Then
        Throw New ArgumentNullException("SignAllParts(package)")
    End If

    ' Create the DigitalSignature Manager
    Dim dsm As New PackageDigitalSignatureManager(package)
    dsm.CertificateOption = CertificateEmbeddingOption.InSignaturePart

    ' Create a list of all the part URIs in the package to sign
    ' (GetParts() also includes PackageRelationship parts).
    Dim toSign As New System.Collections.Generic.List(Of Uri)()
    For Each packagePart As PackagePart In package.GetParts()
        ' Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri)
    Next

    ' Add the URI for SignatureOrigin PackageRelationship part.
    ' The SignatureOrigin relationship is created when Sign() is called.
    ' Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin))

    ' Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin)

    ' Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(New Uri("/", UriKind.RelativeOrAbsolute)))

    ' Sign() will prompt the user to select a Certificate to sign with.
    Try
        dsm.Sign(toSign)
    Catch ex As CryptographicException

        ' If there are no certificates or the SmartCard manager is
        ' not running, catch the exception and show an error message.
        MessageBox.Show("Cannot Sign" & vbLf & ex.Message, "No Digital Certificates Available", MessageBoxButton.OK, MessageBoxImage.Exclamation)

    End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
' end:SignAllParts()

Aplica-se a

Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>)

Assina uma lista de partes e relações do pacote com um determinado certificado X.509.

public:
 System::IO::Packaging::PackageDigitalSignature ^ Sign(System::Collections::Generic::IEnumerable<Uri ^> ^ parts, System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate, System::Collections::Generic::IEnumerable<System::IO::Packaging::PackageRelationshipSelector ^> ^ relationshipSelectors);
public System.IO.Packaging.PackageDigitalSignature Sign (System.Collections.Generic.IEnumerable<Uri> parts, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Collections.Generic.IEnumerable<System.IO.Packaging.PackageRelationshipSelector> relationshipSelectors);
member this.Sign : seq<Uri> * System.Security.Cryptography.X509Certificates.X509Certificate * seq<System.IO.Packaging.PackageRelationshipSelector> -> System.IO.Packaging.PackageDigitalSignature
Public Function Sign (parts As IEnumerable(Of Uri), certificate As X509Certificate, relationshipSelectors As IEnumerable(Of PackageRelationshipSelector)) As PackageDigitalSignature

Parâmetros

parts
IEnumerable<Uri>

A lista de URIs (Uniform Resource Identifiers) para os objetos PackagePart a serem assinados.

certificate
X509Certificate

O certificado X.509 a ser usado para assinar digitalmente cada uma das partes e das relações especificadas.

relationshipSelectors
IEnumerable<PackageRelationshipSelector>

A lista de objetos PackageRelationship a ser assinada.

Retornos

A assinatura digital usada para assinar os elementos especificados nas listas parts e relationshipSelectors.

Exceções

O parts e o relationshipSelectors não especificam nenhum objeto para ser assinado.

Exemplos

O exemplo a seguir mostra como assinar digitalmente uma lista de partes do pacote. Para obter o exemplo completo, consulte Criando um pacote com um exemplo de assinatura digital.

private static void SignAllParts(Package package)
{
    if (package == null)
        throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager
    PackageDigitalSignatureManager dsm =
        new PackageDigitalSignatureManager(package);
    dsm.CertificateOption =
        CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign
    // (GetParts() also includes PackageRelationship parts).
    System.Collections.Generic.List<Uri> toSign =
        new System.Collections.Generic.List<Uri>();
    foreach (PackagePart packagePart in package.GetParts())
    {
        // Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri);
    }

    // Add the URI for SignatureOrigin PackageRelationship part.
    // The SignatureOrigin relationship is created when Sign() is called.
    // Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));

    // Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin);

    // Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

    // Sign() will prompt the user to select a Certificate to sign with.
    try
    {
        dsm.Sign(toSign);
    }

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
    {
        MessageBox.Show(
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",
            MessageBoxButton.OK,
            MessageBoxImage.Exclamation);
    }
}// end:SignAllParts()
Private Shared Sub SignAllParts(ByVal package As Package)
    If package Is Nothing Then
        Throw New ArgumentNullException("SignAllParts(package)")
    End If

    ' Create the DigitalSignature Manager
    Dim dsm As New PackageDigitalSignatureManager(package)
    dsm.CertificateOption = CertificateEmbeddingOption.InSignaturePart

    ' Create a list of all the part URIs in the package to sign
    ' (GetParts() also includes PackageRelationship parts).
    Dim toSign As New System.Collections.Generic.List(Of Uri)()
    For Each packagePart As PackagePart In package.GetParts()
        ' Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri)
    Next

    ' Add the URI for SignatureOrigin PackageRelationship part.
    ' The SignatureOrigin relationship is created when Sign() is called.
    ' Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin))

    ' Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin)

    ' Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(New Uri("/", UriKind.RelativeOrAbsolute)))

    ' Sign() will prompt the user to select a Certificate to sign with.
    Try
        dsm.Sign(toSign)
    Catch ex As CryptographicException

        ' If there are no certificates or the SmartCard manager is
        ' not running, catch the exception and show an error message.
        MessageBox.Show("Cannot Sign" & vbLf & ex.Message, "No Digital Certificates Available", MessageBoxButton.OK, MessageBoxImage.Exclamation)

    End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
' end:SignAllParts()

Comentários

Entre parts e relationshipSelectors deve haver pelo menos um elemento para assinar.

Aplica-se a

Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>, String)

Assina uma lista de partes do pacote e relações de pacote com um determinado certificado X.509 e identificação (ID).

public:
 System::IO::Packaging::PackageDigitalSignature ^ Sign(System::Collections::Generic::IEnumerable<Uri ^> ^ parts, System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate, System::Collections::Generic::IEnumerable<System::IO::Packaging::PackageRelationshipSelector ^> ^ relationshipSelectors, System::String ^ signatureId);
public System.IO.Packaging.PackageDigitalSignature Sign (System.Collections.Generic.IEnumerable<Uri> parts, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Collections.Generic.IEnumerable<System.IO.Packaging.PackageRelationshipSelector> relationshipSelectors, string signatureId);
member this.Sign : seq<Uri> * System.Security.Cryptography.X509Certificates.X509Certificate * seq<System.IO.Packaging.PackageRelationshipSelector> * string -> System.IO.Packaging.PackageDigitalSignature
Public Function Sign (parts As IEnumerable(Of Uri), certificate As X509Certificate, relationshipSelectors As IEnumerable(Of PackageRelationshipSelector), signatureId As String) As PackageDigitalSignature

Parâmetros

parts
IEnumerable<Uri>

A lista de URIs (Uniform Resource Identifiers) para os objetos PackagePart a serem assinados.

certificate
X509Certificate

O certificado X.509 a ser usado para assinar digitalmente cada uma das partes e das relações especificadas.

relationshipSelectors
IEnumerable<PackageRelationshipSelector>

A lista de objetos PackageRelationship a ser assinada.

signatureId
String

Uma cadeia de caracteres de identificação a ser associada à assinatura.

Retornos

A assinatura digital usada para assinar os elementos especificados nas listas parts e relationshipSelectors.

Exceções

O parts e o relationshipSelectors não especificam nenhum elemento para ser assinado.

Exemplos

O exemplo a seguir mostra como assinar digitalmente uma lista de partes do pacote. Para obter o exemplo completo, consulte Criando um pacote com um exemplo de assinatura digital.

private static void SignAllParts(Package package)
{
    if (package == null)
        throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager
    PackageDigitalSignatureManager dsm =
        new PackageDigitalSignatureManager(package);
    dsm.CertificateOption =
        CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign
    // (GetParts() also includes PackageRelationship parts).
    System.Collections.Generic.List<Uri> toSign =
        new System.Collections.Generic.List<Uri>();
    foreach (PackagePart packagePart in package.GetParts())
    {
        // Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri);
    }

    // Add the URI for SignatureOrigin PackageRelationship part.
    // The SignatureOrigin relationship is created when Sign() is called.
    // Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));

    // Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin);

    // Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

    // Sign() will prompt the user to select a Certificate to sign with.
    try
    {
        dsm.Sign(toSign);
    }

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
    {
        MessageBox.Show(
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",
            MessageBoxButton.OK,
            MessageBoxImage.Exclamation);
    }
}// end:SignAllParts()
Private Shared Sub SignAllParts(ByVal package As Package)
    If package Is Nothing Then
        Throw New ArgumentNullException("SignAllParts(package)")
    End If

    ' Create the DigitalSignature Manager
    Dim dsm As New PackageDigitalSignatureManager(package)
    dsm.CertificateOption = CertificateEmbeddingOption.InSignaturePart

    ' Create a list of all the part URIs in the package to sign
    ' (GetParts() also includes PackageRelationship parts).
    Dim toSign As New System.Collections.Generic.List(Of Uri)()
    For Each packagePart As PackagePart In package.GetParts()
        ' Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri)
    Next

    ' Add the URI for SignatureOrigin PackageRelationship part.
    ' The SignatureOrigin relationship is created when Sign() is called.
    ' Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin))

    ' Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin)

    ' Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(New Uri("/", UriKind.RelativeOrAbsolute)))

    ' Sign() will prompt the user to select a Certificate to sign with.
    Try
        dsm.Sign(toSign)
    Catch ex As CryptographicException

        ' If there are no certificates or the SmartCard manager is
        ' not running, catch the exception and show an error message.
        MessageBox.Show("Cannot Sign" & vbLf & ex.Message, "No Digital Certificates Available", MessageBoxButton.OK, MessageBoxImage.Exclamation)

    End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
' end:SignAllParts()

Comentários

A parts lista pode estar vazia ou null se relationshipSelectors contiver pelo menos uma entrada.

A relationshipSelectors lista pode estar vazia ou null se parts contiver pelo menos uma entrada.

Entre a parts lista e relationshipSelectors deve haver pelo menos um elemento a ser assinado.

Aplica-se a

Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>, String, IEnumerable<DataObject>, IEnumerable<Reference>)

Assina uma lista de partes do pacote, relações de pacote ou objetos personalizados com um certificado X.509 especificado e uma ID (identificador de assinatura).

public:
 System::IO::Packaging::PackageDigitalSignature ^ Sign(System::Collections::Generic::IEnumerable<Uri ^> ^ parts, System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate, System::Collections::Generic::IEnumerable<System::IO::Packaging::PackageRelationshipSelector ^> ^ relationshipSelectors, System::String ^ signatureId, System::Collections::Generic::IEnumerable<System::Security::Cryptography::Xml::DataObject ^> ^ signatureObjects, System::Collections::Generic::IEnumerable<System::Security::Cryptography::Xml::Reference ^> ^ objectReferences);
[System.Security.SecurityCritical]
public System.IO.Packaging.PackageDigitalSignature Sign (System.Collections.Generic.IEnumerable<Uri> parts, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Collections.Generic.IEnumerable<System.IO.Packaging.PackageRelationshipSelector> relationshipSelectors, string signatureId, System.Collections.Generic.IEnumerable<System.Security.Cryptography.Xml.DataObject> signatureObjects, System.Collections.Generic.IEnumerable<System.Security.Cryptography.Xml.Reference> objectReferences);
public System.IO.Packaging.PackageDigitalSignature Sign (System.Collections.Generic.IEnumerable<Uri> parts, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Collections.Generic.IEnumerable<System.IO.Packaging.PackageRelationshipSelector> relationshipSelectors, string signatureId, System.Collections.Generic.IEnumerable<System.Security.Cryptography.Xml.DataObject> signatureObjects, System.Collections.Generic.IEnumerable<System.Security.Cryptography.Xml.Reference> objectReferences);
[<System.Security.SecurityCritical>]
member this.Sign : seq<Uri> * System.Security.Cryptography.X509Certificates.X509Certificate * seq<System.IO.Packaging.PackageRelationshipSelector> * string * seq<System.Security.Cryptography.Xml.DataObject> * seq<System.Security.Cryptography.Xml.Reference> -> System.IO.Packaging.PackageDigitalSignature
member this.Sign : seq<Uri> * System.Security.Cryptography.X509Certificates.X509Certificate * seq<System.IO.Packaging.PackageRelationshipSelector> * string * seq<System.Security.Cryptography.Xml.DataObject> * seq<System.Security.Cryptography.Xml.Reference> -> System.IO.Packaging.PackageDigitalSignature
Public Function Sign (parts As IEnumerable(Of Uri), certificate As X509Certificate, relationshipSelectors As IEnumerable(Of PackageRelationshipSelector), signatureId As String, signatureObjects As IEnumerable(Of DataObject), objectReferences As IEnumerable(Of Reference)) As PackageDigitalSignature

Parâmetros

parts
IEnumerable<Uri>

A lista de URIs (Uniform Resource Identifiers) para os objetos PackagePart a serem assinados.

certificate
X509Certificate

O certificado X.509 a ser usado para assinar digitalmente cada uma das partes e das relações especificadas.

relationshipSelectors
IEnumerable<PackageRelationshipSelector>

A lista de objetos PackageRelationship a ser assinada.

signatureId
String

Uma cadeia de caracteres de identificação a ser associada à assinatura.

signatureObjects
IEnumerable<DataObject>

Uma lista de objetos de dados personalizados a ser assinada.

objectReferences
IEnumerable<Reference>

Uma lista de referências a objetos personalizados a ser assinada.

Retornos

A assinatura digital usada para assinar os elementos especificados nas listas parts e relationshipSelectors.

Atributos

Exceções

parts, relationshipSelectors, signatureObjects e objectReferences não especificam nenhum elemento a ser assinado.

Um ContentType de uma parte que está sendo assinada faz referência a um vazio, null ou TransformMapping indefinido.

signatureId não null é e não é uma ID de esquema XML válida (por exemplo, começa com um dígito numérico à esquerda).

Comentários

Deve haver pelo menos um elemento para entrar, partsrelationshipSelectors, signatureObjectsou objectReferences.

Observação

Os termos Object, Manifest, Reference, SignaturePropertiese Transform nas duas observações a seguir referem-se a tipos de elemento e marcas definidas pela sintaxe de XML-Signature W3C e especificação de processamento, consulte https://www.w3.org/TR/xmldsig-core/.

Essa e outras Sign sobrecargas de método usam o dicionário atual TransformMapping que define um Transform a ser aplicado com base na parte ContentTypedo pacote . A especificação OPC (Convenções de Empacotamento Aberto) da Microsoft atualmente permite apenas dois algoritmos válidos Transform : C14 e C14N. A sintaxe XML-Signature W3C e o padrão de processamento não permitem marcas vazias Manifest . Além disso, a especificação Convenções de Empacotamento Aberto requer uma Packagemarca específica Object que contém marcas Manifest e SignatureProperties . Além Manifest disso, cada marca também inclui pelo menos uma Reference marca. Essas marcas exigem que cada assinatura assine pelo menos uma PackagePart (marca de partes não vazias) ou PackageRelationship (não vazia relationshipSelectors), mesmo que a assinatura seja necessária apenas para assinar signatureObjects ou objectReferences.

Esse Sign método ignora a DigestMethod propriedade associada a cada Reference definida em objectReferences.

Essa Sign sobrecarga fornece suporte para a geração de assinaturas XML que exigem marcas personalizadas Object . Para que qualquer marca fornecida Object seja assinada, uma marca correspondente Reference deve ser fornecida com um URI (uniform resource identifier) que especifica a Object marca na sintaxe de fragmento local. Por exemplo, se a Object marca tiver uma ID de "myObject", o URI na Reference marca será "#myObject". Para objetos não assinados, não Reference é necessário.

Aplica-se a