An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
I found how to fix the issue directly using the PackageDigitalSignatureManager
The example code I used is from:
And I added
// Package part relationships must also be added otherwise you get only a partial signature
foreach (PackageRelationship relationship in packagePart.GetRelationships())
{
relationshipSelectors.Add(new
PackageRelationshipSelector(relationship.SourceUri,
PackageRelationshipSelectorType.Id, relationship.Id));
}
Here's the digital signature method in its entirety:
private static bool SignOfficeDoc(string officeDocPackagePath, X509Certificate2 certificate)
{
using (var package = Package.Open(officeDocPackagePath))
{
var signatureManager = new PackageDigitalSignatureManager(package);
signatureManager.CertificateOption = CertificateEmbeddingOption.InSignaturePart;
//Initialize a list to hold the part URIs to sign.
List<Uri> partsToSign = new List<Uri>();
//Create list of selectors for the list of relationships
List<PackageRelationshipSelector> relationshipSelectors =
new List<PackageRelationshipSelector>();
//Add each part to the list, except relationships parts.
foreach (PackagePart packagePart in package.GetParts())
{
if (!PackUriHelper.IsRelationshipPartUri(packagePart.Uri))
{
partsToSign.Add(packagePart.Uri);
// Package part relationships must also be added otherwise you get only a partial signature
foreach (PackageRelationship relationship in packagePart.GetRelationships())
{
relationshipSelectors.Add(new
PackageRelationshipSelector(relationship.SourceUri,
PackageRelationshipSelectorType.Id, relationship.Id));
}
}
}
//Create one selector for each package-level relationship, based on id
foreach (PackageRelationship relationship in package.GetRelationships())
{
relationshipSelectors.Add(new
PackageRelationshipSelector(relationship.SourceUri,
PackageRelationshipSelectorType.Id, relationship.Id));
}
PackageDigitalSignature signature = signatureManager.Sign(partsToSign, certificate, relationshipSelectors);
//After signing, close the package.
//The signature will be persisted in the package.
package.Close();
}
return true;
}