This documentation is archived and is not being maintained.

PackageDigitalSignatureManager.Sign Method

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

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  NameDescription
Public method Sign(IEnumerable<Uri>) Prompts the user for an X.509 certificate, which is then used to digitally sign a specified list of package parts.
Public method Sign(IEnumerable<Uri>, X509Certificate) Signs a list of package parts with a given X.509 certificate.
Public method Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>) Signs a list of package parts and package relationships with a given X.509 certificate.
Public method Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>, String) Signs a list of package parts and package relationships with a given X.509 certificate and identifier (ID).
Public method Sign(IEnumerable<Uri>, X509Certificate, IEnumerable<PackageRelationshipSelector>, String, IEnumerable<DataObject>, IEnumerable<Reference>) Signs a list of package parts, package relationships, or custom objects with a specified X.509 certificate and signature identifier (ID).
Top

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 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.
    Uri uriPartSignatureOriginRelationship = PackUriHelper.CreatePartUri(
        new Uri(_digitalSignatureUri, UriKind.Relative));
    toSign.Add(uriPartSignatureOriginRelationship);

    // 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()
Show: