PackageDigitalSignatureManager.Sign Method


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Signs a list of package parts with a specified X.509 certificate.

Namespace:   System.IO.Packaging
Assembly:  WindowsBase (in WindowsBase.dll)

System_CAPS_pubmethodSign(IEnumerable(Of Uri))

Prompts the user for an X.509 certificate, which is then used to digitally sign a specified list of package parts.

System_CAPS_pubmethodSign(IEnumerable(Of Uri), X509Certificate)

Signs a list of package parts with a given X.509 certificate.

System_CAPS_pubmethodSign(IEnumerable(Of Uri), X509Certificate, IEnumerable(Of PackageRelationshipSelector))

Signs a list of package parts and package relationships with a given X.509 certificate.

System_CAPS_pubmethodSign(IEnumerable(Of Uri), X509Certificate, IEnumerable(Of PackageRelationshipSelector), String)

Signs a list of package parts and package relationships with a given X.509 certificate and identifier (ID).

System_CAPS_pubmethodSign(IEnumerable(Of Uri), X509Certificate, IEnumerable(Of PackageRelationshipSelector), String, IEnumerable(Of DataObject), IEnumerable(Of Reference))

Signs a list of package parts, package relationships, or custom objects with a specified X.509 certificate and signature identifier (ID).

The following example shows the steps to digitally sign a list of parts within a Package. For the complete sample, see Creating a Package with a Digital Signature Sample.

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.

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

    ' Also sign the SignatureOrigin part.

    ' 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.
    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()
Return to top