3

Background

I have an ASP.net MVC 4 web application with Forms authentication and a custom AuthorizeAttribute controlling access to all controllers minus the login screen. I am adding to some of the controllers, an action that allows the user to download a server generated PDF whose content and layout is being defined in a Razor View.

To carry out the conversion between Html and PDF, im using a trial version of ABCPdf9 and the bulk of the conversion works perfectly with all CSS, Text, static images, etc being displayed as required.

The problem is that I have images in the HTML that must also be rendered into the PDF file, but these images come from a controller which requires authentication.

Since the Html to PDF conversion takes place in ABCPdf using the Gecko engine, the existing user authentication cookies, etc are not available and as such the GET requests to the Image Controller are not authenticated and will not return anything. (This is the problem).

What I've discovered

From the research I have done, I came across the HttpAdditionalHeaders Property of ABCPdf which (from my understanding) is there to allow you to set header cookies, etc that will be sent with the requests made by the Gecko engine when fetching resources.

I have spent a good few hours trying to set the existing cookies from the originating request and pass them through to ABCPdf but this does not appear to work. Nor does creating a new set of authenticated cookies and pass them either.

So from what I gather, this solution is not possible....

My question

Does anyone know if it is possible to modify the Url.Action helper so it will generate and include a one-time authentication key in the url? Then implement some code in my custom AuthorizeAttribute that strip the key parameter and if valid, provide an alternative method of authentication to access the necessary image controller. This way, I can run the ABCPdf conversion process and it will be able to access the normally restricted resources in the image controller.

For example:

<img src="http://somesite.com/Image/Retrieve/1234?key=WFD6312DFV154WHSF3B1SGB69SB" />

The custom AuthorizeAttribute code should then recognise the key parameter passed in the requests query string and then bypass existing authentication processes.

Any help or even suggestions for where to look would be greatly appreciated!

JohnHenry
  • 505
  • 8
  • 21
  • This does not answer your question but i've created a free razor html to pdf convertor for mvc. Please see here: http://stackoverflow.com/questions/16517171/convert-html-to-pdf-in-mvc-with-itextsharp-in-mvc-razor/20198939#20198939 I would be interested on some feedback from people. Just ignore if it's of no use. :) – hutchonoid Nov 26 '13 at 16:43

0 Answers0