Hi,
I used the following code from the Windows SDK to digitally sign Word Documents but When I open the Signed documents in Word, Word is complaining it can't verify the Signatures and I notices that the Package folder and relatinonship entries are different when using the Word UI to digitally sign a document compared to using the Packaging API, is there anything different that needs to be done compared to the code below from the June CTP of the Windows SDK ?
private void Sign(Package package)
{
string _digitalSignatureUri =
"/package/services/digital-signature/_rels/origin.psdsor.rels";
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);
}
}
Thanks,
Krishna