Welcome Guest Search | Active Topics | Sign In | Register

HtmlToPdf: How to access cookies to make authenticated ajax calls Options
Devin
Posted: Tuesday, November 1, 2016 6:32:28 PM
Rank: Newbie
Groups: Member

Joined: 11/1/2016
Posts: 5
Hi,

I have an application that uses ASP.NET MVC 5 Identity cookie authentication. I'm trying to create a .pdf of a view that makes multiple ajax calls to pull data that also sit behind the cookie authentication wall. When my view loads, the cookies that I've applied with HtmlToPdf.Options.Cookies disappear and so all of my subsequent ajax calls fail. To test, I'm printing out document.cookie to a div and immediately returning with a call to eoapi.convert(). I've tried both HtmlToPdf.ConvertUrl and the [RenderAsPDF] attribute methods of creating a pdf. Is there a way to let my page keep its cookies while it finishes executing JavaScript like a normal browser?

Thanks!
Devin
Posted: Thursday, November 3, 2016 12:23:33 PM
Rank: Newbie
Groups: Member

Joined: 11/1/2016
Posts: 5
Safe to assume EO.pdf does not support this feature based on the lack of replies?
eo_support
Posted: Thursday, November 3, 2016 12:29:02 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

Sorry that we haven't relied the post yet because we are still working on it. Internally MVCToPDF calls ConvertHtml, not ConvertUrl to perform the conversion, which might have implications with cookies. We are still looking into it and as soon as we have a definite answer we will post again.

Thanks!
Devin
Posted: Thursday, November 3, 2016 12:36:31 PM
Rank: Newbie
Groups: Member

Joined: 11/1/2016
Posts: 5
Ok, thank you! I originally tried to use HtmlToPdf.ConvertUrl() and not MVCToPDF, but found the same behavior in both configurations.
eo_support
Posted: Thursday, November 3, 2016 5:43:55 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

We have looked into this issue. The fact that you do not see document.cookie can be normal. If you have JavaScript code that explicitly depends on document.cookie, then you may need to change that. Below is a detailed explanation of why this would occur and hopefully that can help you find a solution.

The root of the problem is there are two different connections to your web server:

1. The connection from your client browser to your web server. You web client browser can be any web browser, for example, it can be IE browser engine. This engines runs on your client computer;
2. The connection from the HTML to PDF converter to your web server. Internally HTML to PDF runs chromium browser engine. This engine runs on your web server;

These two completely physically separate browser engines obviously have their own separate cookie stores, from which document.cookie gets its value. In another word, document.cookie value inside your client side IE browser are completely unrelated to document.cookie value inside the chromium browser engine hosted by the HTML to PDF engine on your web server.

MVCToPDF does automatically duplicate your ASP.NET authentication cookies and attach those cookies to the requests made by the HTML to PDF converter. This makes the two connections "appear" to be the same from your web server's point of view since requests from both connections will contain the same authentication cookie. However this does not change the contents of the cookie stores maintained by either engine. Since document.cookie is based on the engine's cookie store, they can show different values.

The easiest way for you to workaround this problem is to use a traffic monitor to find out which cookie is missing from your AJAX requests and pass them explicitly to the HTML to PDF converter through HtmlToPdf.Options.Cookies (make sure you set it in your PreRender handler). Once you supply those cookies, your AJAX calls should work even though document.cookie will still be empty.

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!
Devin
Posted: Thursday, November 3, 2016 7:47:15 PM
Rank: Newbie
Groups: Member

Joined: 11/1/2016
Posts: 5
Hi,

Thanks for the explanation. I do understand the different between the cookie stores. I tried to attach my cookies via the PreRender handler (when using MVCToPDF) and before the call to HtmlToPdf.ConvertUrl() (when not using MVCToPDF). Here's an example of the PreRender handler:

Code: C#
private void Pre_Pdf_Handler(object sender, EventArgs e)
        {
            foreach (var key in this.Request.Cookies.AllKeys.Distinct().Where(k => !k.ToLower().Contains("authentication")))
            {
                if (!string.IsNullOrEmpty(this.Request.Cookies[key].Value))
                    HtmlToPdf.Options.Cookies.Add(new System.Net.Cookie(key, this.Request.Cookies[key].Value));
            }

            HtmlToPdf.Options.NoScript = false;
            HtmlToPdf.Options.TriggerMode = HtmlToPdfTriggerMode.Dual;
            HtmlToPdf.Options.UsePrintMedia = true;
            HtmlToPdf.Options.AutoFitX = HtmlToPdfAutoFitMode.ShrinkToFit;
            HtmlToPdf.Options.PageSize = new SizeF(11f, 8.5f);
            HtmlToPdf.Options.MaxLoadWaitTime = 8000;
        }


The above does not seem to add the cookies to a JavaScript-accessible store in the internal browser engine, as a print of document.cookie suggests. Are you saying you are keeping them in the HTML to PDF process somewhere and attaching them before each ajax call, but they never end up accessible by JavaScript? I do have a cookie value(s) that I need to access with JavaScript.

Thanks!
eo_support
Posted: Friday, November 4, 2016 7:41:56 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

Yes. That's exactly what my previous reply is about. document.cookie will not have those cookies. Here is a "normal" flow when you access the page directly through a browser:

1. The local cookie store empty (thus document.cookie is empty as well);
2. You login through login page;
3. The server sends an authentication cookies to the client, this cookie is added to the local cookie store;
4. document.cookie reflects this value since it gets its value from the local cookie store;

The flow for HTML to PDF conversion is:

1. The conversion engine's cookie store is empty;
2. Before the converter sends out a request to the web server, it collects cookies from:
2.a. The conversion engine's local cookie store, which is empty;
2.b. HtmlToPdf.Options.Cookies, this contains your authentication cookies;
3. The server verifies the cookie and send the response down to the client;

Note here on step 3 the server DOES NOT send an authentication cookie to the HTML to PDF converter browser engine since it sees that request already have the authentication cookie. As a result, the browser engine's cookie store will never have the cookie. This means document.cookie will never have the value.

Hope this makes sense to you.

Thanks!
Devin
Posted: Friday, November 4, 2016 11:48:54 AM
Rank: Newbie
Groups: Member

Joined: 11/1/2016
Posts: 5
Ah, ok, thanks again!

Devin


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.