Authentication Handling

The web page you want to convert might be protected by different types of authentication. The most common authentication methods are Integrated Windows Authentication, Forms Authentication and custom login pages. The HiQPdf HTML to PDF Converter offers support for resolving all these types of authentication.

Integrated Windows Authentication (NTLM)

The converter automatically uses the credentials of the user running the converter to resolve NTLM authentication. This user can be the currently logged-in user when using the converter in a desktop application, or the user set as the IIS application pool identity when using the converter in an ASP.NET application. If the default automatic credentials cannot resolve the authentication, you can explicitly set the Username and Password in the HtmlToPdfAuthentication or HtmlToImageAuthentication objects, depending on the interface you are using to convert HTML to PDF or images.

Code Sample - Explicitly Set the Authentication Options

C#
// Create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// Set authentication options
htmlToPdfConverter.Authentication.Username = username;
htmlToPdfConverter.Authentication.Password = password;

// Create the HTML to Image converter
HtmlToImage htmlToImageConverter = new HtmlToImage();
// Set authentication options
htmlToImageConverter.Authentication.Username = username;
htmlToImageConverter.Authentication.Password = password;

Cookie-Based Authentication

Cookie-based authentication implementations usually store the forms authentication ticket in a cookie, which must be sent back to the server each time a resource is requested. Authentication cookies can be sent back to the server using the HtmlToPdfHttpCookies or HtmlToImageHttpCookies properties, depending on the interface you are using to convert HTML to PDF or images.

Code Sample - Explicitly Set the Forms Authentication Cookie

C#
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

// Add the authentication cookie to the request
htmlToPdfConverter.HttpCookies.Add(AuthCookieName, AuthCookieValue);

htmlToPdfConverter.ConvertUrlToMemory(urlToConvert);

Login Page Authentication

Authentication implemented at the application level using a login page can be resolved by first retrieving the HTML code of the web page to be converted and then converting that HTML string to PDF.

The CSS files and images referenced by the HTML code to be converted should be placed in a location that does not require authentication. Otherwise, you must combine this approach with one of the authentication methods described above to resolve those resources.

See Also