Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have spent days trying to get a PDF generating application to generate PDF's from an ASP.NET password protected site using Forms Athentication. The question is really how can any 3rd party application be authorised under forms authentication?

Here is the suppliers explanation of what to do, which I do not undersatnd.

"Under most authentication, a session ID is returned for subsequent requests so re-authentication is not needed. You may want to look at the cookies in the response and set the cookies so that ABCpdf's request contains the session-ID cookies.
To obtain the cookies, you'll need to use a new WebRequest (HttpWebRequest) to request something from your target site. You can pass in whatever credentials desired and you'll need to initialize the request's CookieContainer property (so that the property is not null) before you get the response. You can then set the cookies from CookieContainer with the WinINet function InternetSetCookie."

They have tried to help, but this is beyond me. Has anyone got this working under a password protected site that can help...please?

The problem is reading the ULR. I can generate a PDF, but all I get is a PDF of login.aspx. Forms Authentication will not let ABCPDF.NET open the web page.

VB
Dim theDoc As Doc = New Doc()

theDoc.AddImageUrl("http://XXXXXXXX/App_Auth_Print.aspx")
theDoc.Save(Server.MapPath("htmlimport1.pdf"))
theDoc.Clear()


"theDoc.AddImageUrl" as an unauthorised request is always redirected to login.aspx.
Posted
Updated 2-Mar-10 9:59am
v2

The only place in-process file writing can be done is in the temp directory (at least by default).

You can find this Directory with System.IO.Path.GetTempPath.

If you want to allow writing to other directories you will have to give IUSR_ write permissions, though I heartily recommend AGAINST doing that.

So the short answer to your question is either

  • Generate the pdf out of process (like send the request to an NT Service etc.)
  • Generate the PDF in the temp directory. You will need to read the file in and send it to the client separately though, since the user won't be able to be able to get to the file via a URL.
 
Share this answer
 
Alaric,

Thank you for the reply, I really appreciate your time. But I can create the PDF files using ABCPDF.NET. I have created the file at least 100 times, but its always of the login page! ABCPDF requires the following code to open a page in the website, and convert the HMTL to a PDF image.

VB
Dim theDoc As Doc = New Doc()

theDoc.AddImageUrl("http://XXXXXXXXX/App_Auth_Print.aspx")
theDoc.Save(Server.MapPath("htmlimport1.pdf"))
theDoc.Clear()



The problem is "theDoc.AddImageUlr has not been through the authorisation process, and is therefore redirected to login.aspx.

I am not very confident with cookies and authorisation, and I have tried to programmatically authorise this request using FormsAuthentication.Authenticate(username.Text, password.Text), passing the username and password encripted via the PrintSecurityID Querystring. But all I get is the login screen as a PDF! Its driving me mad!
 
Share this answer
 
I see, I misunderstood the problem. Essentially your problem here seems to be that you are coming in from session that isn't the users, thus it is always directing you to the login screen.

In this case you are dealing with cookies, authentication cookies. And this is a little beyond my expertise but I found this.

Pass-Through Security Authentication (Single Sign-on) using ASP.NET

and this (which looks more like what you want)

How To: Pass Authentication Between Two ASP.NET Applications

Personally I think I would enable a 'momentary bypass'. Create a unique token, one that times out in a few seconds and pass it in a "post". Thus allowing me to bypass the login.

The last thing you might try is the WCF authentication service.
 
Share this answer
 
Alaric,

Thank you again for the reply, it is much appreciated. The links are very useful, and close to the approach I started with. I had tried to pass an encoded parameter with the ULR, and this morning I have found then reason this does not work, ABCPDF strips off parameters unless another method is envoked. Along with your suggested approaches, I think I can now get it to work. Thanks again.
 
Share this answer
 
can you please let us know what the soultion is..i have the same issue and it is driving me crazy...thanks.
pushya.
 
Share this answer
 
Sorry I just came back to this thread. The solution was a whole series of tests and adjustments to both the code and the server too complex to list out. Which is a shame because I have just moved the sites to a new server and have the same issue. As soon as the sites moved to the new server, authorisation stopped working. Back to square one!!!
 
Share this answer
 
Ok, Since there were no responses and I ran within the team and tried and tested for over 4 weeks, here is what we did as far as we can rememeber or work out (As much for me as anyone else:)).

1. Forget about integrated 4 on iis 7, we changed and used classic 2.0 - it did not work and we tried ALL the advice.
2. Could not get the geko engine working, even when we followed all the advice, default printer, bin files, .ddl installation etc. (more importantly there are restrictions with the geko engine in regards to css and the ability to use print.css rather than display.css).
3. This will work for a while, and when you think all is well, you wake up its gone wrong again (Cannot activate MSHtml engine. Please refer to documentation for more information), go into iis and:

IN the IIS Application pool, Select the application (abcpdf.net) go to advance setting and Property Load user profile = true; (thank you to a fellow contributor)

4. So far its been working for about 4 weeks

If you are using master pages and linking your css via the master page, you will be told not to use relative links (~/etc/etc), but absolute (../../). It does not work. When you send anything to print via abcpdf.net server side, use a querystring or something to identify the app and swap the .css file on the page init. for example:

VB
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Dim css As New HtmlLink()
    If Request.QueryString("PrintID") Is Nothing Then
    Else
        css.Href = "~/print.css"
        css.Attributes("rel") = "stylesheet"
        css.Attributes("type") = "text/css"
        css.Attributes("media") = "all"
        Page.Header.Controls.Add(css)
    End If
End Sub


Thats about it.

Your pages will be printed exactly like hitting a page preview in an browser, except where you are using javascript to adjust client side controls (like dev express component), when you can expect more fun!

The original question was about authorisation when using a password protected site and attempting to print pages within this site. ABCPDF will be treated like a new user and you will need to authorise from your code behind or you will end up with prints of your log in page. Our application bans persistent sessions, so we did not and could not follow the advice from abcpdf.net (who are very helpful). We did this which may not be elegant but...

1. When a user clicks a print button create an id (GUID perhaps) within your database (or may be a file you can open and reference),
2. Set up a known user (with minimum rights) and encrypt the credentials in the code behind,
3. Pass the id (encrypted) to a querystring when ABCPDF.NET attempts to open a page eg,

created_URL = url & pagename & "?PrintID=" & printid ' encrypted
theID = thedoc.AddImageUrl(created_URL)

4. In each page, if the id can be found within the database, use the encrypted known user, decrypt and authorise (if it can't redirect the user to an abuse page)
5. Print the page. You will need this page load event in all pages (but you can use the same event for other things). You may need to authorise once, you may need to do this on each page (we created a printhelper.vb to deal with this including the encrypted user credentials)
6. Delete the print id so it cant be used again

No, its not as elegant as it could be. But it all happens server side and is encrypted, and you can use the ID event for other monitoring etc. Oh yes...it works!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900