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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

PDF signing in Azure Web App returns 502

Thread ID:

Created:

Updated:

Platform:

Replies:

132166 Aug 17,2017 08:31 PM UTC Aug 18,2017 01:17 PM UTC ASP.NET MVC 1
loading
Tags: PDF
Scott
Asked On August 17, 2017 08:32 PM UTC

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. 

Sasi Kumar Sekar [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;