There are situations when you want to convert web pages that require authentication when accessed. The HiQPdf HTML to PDF Converter can handle various authentication methods. The most important authentication scenarios that can be handled by the converter are detailed in the sections below.
When a web page require HTTP authentication and you access that page in browser the browser will prompt you to enter a username and a password. Similar, the HiQPdf converters have the Authentication property which exposes an object with the Username and Password properties that can be set with the same values you would enter in a browser.
An important example of HTTP authentication is the Integrated Windows Authentication in IIS, formerly called NTLM authentication. By default the converter will try to use the crendentials of the user running the converter because the UseCurrentUserCredentials property is true by default. If you want to use the credentials of another user you have to set the Username and Password properties with the username and password of a Windows account having access to the page being converted and in this case the UseCurrentUserCredentials property will be ignored.
With forms authentication, user information is stored in an external data source, such as a membership database, or in the configuration file for an application. Once a user is authenticated, forms authentication maintains an authentication ticket in a cookie or in the URL so that an authenticated user does not need to supply credentials with each request.
When you are already authenticated and you want to convert a page of the same application you can simply set the authentication cookie before converting the web page. A cookie can be sent to the web server server as documented in the HTTP Cookies section. A simple code to create a HTML to PDF converter object, set the forms authentication cookie and convert an URL to a memory buffer is given below:
HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); htmlToPdfConverter.HttpCookies.AddCookie( FormsAuthentication.FormsCookieName, Request.Cookies[FormsAuthentication.FormsCookieName].Value ); byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
When the application is implementing a custom authentication mechanism, like storing the logged in user credentials in ASP.NET session and verifying the storred credentials at each page access, a possible solution to handle this type of authentication is to get the HTML code of the web page using the Render method overriding or calling the Server.Execute method as explained in the Get Web Page HTML Code and Convert it to PDF section. As a base URL when converting the HTML code you can use the URL of the web page to be converted.