Web Page Authentication | |
Select.Pdf HTML to PDF Converter can convert any web page a browser can open, however some web pages may require authentication before access can be granted. In such cases additional information must be supplied to the converter so that the converter can login to be granted access to the page.
Authentication Types:
HTTP authentication is handled on the HTTP protocol level. When a page that requires HTTP authentication is accessed, the browser will display a login dialog where the user can enter an username and password. Once the correct user name and password is entered, access is granted and the page is displayed.
Select.Pdf HTML to PDF converter can automatically access pages that require HTTP authentication. To enable this feature, the Username and Password properties of HtmlToPdfAuthenticationOptions object need to be set with a valid user name and password for the page. Select.Pdf HTML to PDF converter will automatically use this information to login and access the page.
The following sample code shows how to convert an url that requires HTTP authentication:
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // set authentication options converter.Options.Authentication.Username = username; converter.Options.Authentication.Password = password; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();
Select.Pdf HTML to PDF supports Integrated Windows Authentication if the NTML provider is used. The current user credentials will be used by the converter to authenticate automatically.
If the automatic windows authentication does not work and the converter cannot resolve the authentication using the current user credentials, the HtmlToPdfAuthenticationOptions object can be used to specify an username and a valid password, like in the above HTTP Authentication section.
Select.Pdf Library for .NET can convert web pages that authenticate the user with a cookie - ASP.NET Core cookie authentication, or ASP.NET Forms Authentication on the .NET Framework.
The converter issues its own request to the page, so it is anonymous unless it is told otherwise. The authentication cookie of the signed in user therefore has to be sent by the converter to the page that will be converted, through HttpCookies.
The cookie name and value are read from the request the application is currently serving. In ASP.NET Core that is the cookie of the cookie authentication handler - .AspNetCore.Cookies by default, or the name configured on its options - read through Request.Cookies. On the .NET Framework it is FormsAuthentication.FormsCookieName.
This sample code shows how to convert an url that requires cookie based authentication from a page of the same application:
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // send the application's authentication cookie to the page being converted, // so the converter is seen by it as the already signed in user converter.Options.HttpCookies.Add(authCookieName, authCookieValue); // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();
In case the web page to be converted is protected by a general login mechanism, the Select.Pdf Html To Pdf Converter will not be able to access the page. To work around this problem, instead of converting the url directly, the page html code should be retrieve using some other method and that html code should be converted to pdf instead of the full url.
The simplest way is to let the application render the page to html itself and convert that string - the same technique described in Current WebPage to Pdf in Asp.Net. Alternatively, the converter can sign in on its own: see Custom Login Page Authentication with Login Options below, which needs no code in the application being converted.
IMPORTANT: If external resources (such as javascript/css files or images) are referenced in the html code, they must be placed into a location where anonymous access is granted, otherwise they will have the same authentication problems and they will not be rendered.
Once the application has the html of the page, the conversion is the same as any other html string conversion:
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // create a new pdf document converting the html the application rendered // (baseUrl resolves the relative resources referenced by that html) PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();
In case the web page to be converted is protected by a general login mechanism, the SelectPdf Html To Pdf Converter can get past the login page if correct username/password and page fields are provided.
This sample code shows how to get past the login page of Facebook:
string url = @"https://www.facebook.com"; // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // set login parameters converter.Options.LoginOptions.LoginPage = @"https://www.facebook.com"; converter.Options.LoginOptions.UsernameFieldSelector = @"#email"; converter.Options.LoginOptions.PasswordFieldSelector = @"#pass"; converter.Options.LoginOptions.SubmitButtonSelector = @"#u_0_2"; converter.Options.LoginOptions.Username = @"PUT YOUR USERNAME HERE"; converter.Options.LoginOptions.Password = @"PUT YOUR PASSWORD HERE"; converter.Options.LoginOptions.DelayBefore = 1000; converter.Options.LoginOptions.DelayAfter = 2000; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();