I have a function for signing and encrypting PDFs
public static Stream DigitallyCertifyPdfStream(Stream uncertifiedFileStream, CertificationBundle certificationBundle)
{
var loadedDocument = new PdfLoadedDocument(uncertifiedFileStream);
var page = loadedDocument.Pages[0] as PdfLoadedPage;
var certificateTemporaryFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid()}.pfx");
File.WriteAllBytes(certificateTemporaryFilePath, certificationBundle.ToBytes());
var certificate = new PdfCertificate(certificateTemporaryFilePath, certificationBundle.Password);
// Signature
var signature = new PdfSignature(loadedDocument, page, certificate, "Signature");
signature.Certificate = certificate;
// Encrypted, password protected PDF
loadedDocument.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
loadedDocument.Security.Algorithm = PdfEncryptionAlgorithm.AES;
loadedDocument.Security.OwnerPassword = certificationBundle.Password;
loadedDocument.Security.Permissions = PdfPermissionsFlags.EditAnnotations | PdfPermissionsFlags.Print | PdfPermissionsFlags.FullQualityPrint;
using (var digitallySignedPdfStream = new MemoryStream())
{
loadedDocument.Save(digitallySignedPdfStream);
loadedDocument.Close(true);
File.Delete(certificateTemporaryFilePath);
return new MemoryStream(digitallySignedPdfStream.ToArray());
}
}
This works fine locally, but when it is deployed to an Azure Web App, it throws a 502 error. I have done remote debugging, and I can consistently make it as far as line
var certificate = new PdfCertificate(certificateTemporaryFilePath, certificationBundle.Password);
but on stepping over that line, the 502 is returned.
My research lead me to
this Syncfusion forum thread from 2014, where the OP concluded he needed to use Web Roles. I would prefer to use plain Azure Web Apps. Have I diagnosed the problem correctly? Is it related to requiring elevated permissions? Is there any way I can get around this? Thanks in advance.