Welcome Guest Search | Active Topics | Sign In | Register

autehentication problem using HtmlToPdf in SPA Options
Maint
Posted: Friday, July 25, 2014 8:59:28 AM
Rank: Newbie
Groups: Member

Joined: 7/25/2014
Posts: 1
When I try to use the HtmlToPdf.ConvertUrl function for a 'view' of my SPA I get a pdf of the login page.

I tried using the HtmlToPdfSession approach (Loading and submitting the login page) and I also tried adding authentication and session cookies to HtmlToPdf.Options.Cookies, and then using the HtmlToPdf.ConvertUrl.
In both cases I get a pdf of the login page (most likely because of a redirect, because the call is not authenticated)

The app uses angular routing, and web-api for the server calls, and needs authentication and session to get the data.

code for first approach:
using (HtmlToPdfSession session = HtmlToPdfSession.Create())
{
session.LoadUrl("login.html");
session.Fill("username", Email);
session.Fill("password", Password);
session.Submit("btnLogin");
session.LoadUrl("http://localhost:34537/reportParticipant/23/122"); // the secure page
session.RenderAsPDF(stream); //StreamContent
}

code second approach:
foreach (string CookieName in HttpContext.Current.Request.Cookies.Keys)
{
System.Net.Cookie Cookie = new System.Net.Cookie(CookieName, HttpContext.Current.Request.Cookies[CookieName].Value);
HtmlToPdf.Options.Cookies.Add(Cookie);
} //the Request.Cookies are HttpCookies
HtmlToPdf.ConvertUrl("http://localhost:34537/reportParticipant/23/122", stream); //StreamContent

So both methods redirect me to the login page. I also have no idea if I can debug the calls done by your dll.

Anybody any thoughts on this?
eo_support
Posted: Friday, July 25, 2014 11:18:54 AM
Rank: Administration
Groups: Administration

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

Your first approach should work if login.html is the login page for the same application and username and password are the only two variables in the page.

Your second approach won't work. Your flow is A -> B -> C. You can't copy cookies generated by B for A and send it to C. C recognizes cookies generated by C, it is not supposed to recognize cookie generated by B.

Regardless using the first approach or the second approach, you will need to understand how your application authenticates users. For example, if your application does not use cookie at all, then obviously the cookie approach won't work. If you do not know all the details about that part, you can try ASPXToPDF control. That control will handle ASP.NET authentication for you (use MVCToPDF if your application is MVC based).

If you do not want to use ASPXToPDF, then there are two things you can do for such cases:

1. Since you are calling localhost, you should be able to debug your server side code to see why the login is refused. Because it's the server that's refusing the login, so only the server can tell you what it needs or why it rejects the login;

2. Try to move the server code to a different machine and then monitor the traffic with a traffic monitor/analyzer. Fiddler is a good one. You can run the application, then run the converter and you should see all HTTP traffic in Fiddler. That should give you more insight about what might go wrong;

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

Thanks!


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.