PackageDigitalSignatureManager.Sign Method (IEnumerable<Uri>, X509Certificate)


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 given X.509 certificate.

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

public PackageDigitalSignature Sign(
	IEnumerable<Uri> parts,
	X509Certificate certificate


Type: System.Collections.Generic.IEnumerable<Uri>

The list of uniform resource identifiers (URIs) for the PackagePart elements to sign.

Type: System.Security.Cryptography.X509Certificates.X509Certificate

The X.509 certificate to use to digitally sign each of the specified parts.

Return Value

Type: System.IO.Packaging.PackageDigitalSignature

The digital signature used to sign the given list of parts; or null if no certificate could be found or the user clicked "Cancel" in the certificate selection dialog box.

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

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 =

    // 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.

    // 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.

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",

}// end:SignAllParts()

.NET Framework
Available since 3.0
Return to top