We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

PDF signing in Azure Web App returns 502

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. 

1 Reply

SK Sasi Kumar Sekar Syncfusion Team August 18, 2017 01:17 PM UTC

Hi Scott, 
 
Thank you for contacting Syncfusion support, 
 
As we have created a web sample using the given code snippet and deployed in Azure web app, initially the issue occurs in PdfCertificate constructor to load the certificate file. For an Azure platform, we have to mention where and how to get the private key of certificate using KeyStorageFlags. Please refer the below code snippet to create PdfCertificate in Azure platform. 
 
Code snippet: 
PdfCertificate certificate = new PdfCertificate (Certificatefilepath, "syncfusion",KeyStorageFlags.MachineKeySet); 
 
Please refer the below link to know more about the KeyStorageFlags. 
We have attached the created sample in following location. 
Sample: 
 
The elevated permission is not required to sign and encrypt the PDF document. The mentioned 502 error is not reproduced in our side. Kindly check the attached above sample and let us know your result. 
 
Thanks, 
Sasi. 


Loader.
Live Chat Icon For mobile
Up arrow icon