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.

  Name Description
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.
            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()
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft