Hi Team,
we recently created two new Windows Servers to host EO library to convert html to PDF. We are getting 401.2 unauthorized error when trying to convert html to PDF when using any URL that requires windows authentication. We currently have 4 servers where the library is working as expected.
We moved the code to a test class to be shared to isolate the problem. We only see an issue when we are trying to access a website that requires windows authentication.
This apps are installed under IIS8, using integrated app pool. We have been comparing the settings between working env vs non working and we haven't been able to pinpoint the issue.
Working machine is running under OS Windows Server 12, non working machine is running on Windows Server 2016. We've been working with our networking team and we haven't yet found the root cause of the problem. IIS settings ( App pool, Website and web app) are the same for the new servers.
We wanted to understand further what settings do we need to have enabled to make windows authentication work internally once chromium launches and tries to connect to the URL. Another interesting fact is , running this code as a test class (logging in to the server and executing) works fine. I believe it's because it's taking the credentials from the logged in user.
Please let us know if you have any thoughts on what we can verify.
Sample code ( Removed license settings). This can be called by passing a url as parameter. Like : Test.aspx?url=http://www.google.com
Code: HTML/ASPX
<%@ Page Language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net.Mime" %>
<%@ Import Namespace="System.Windows" %>
<%@ Import Namespace="System.Windows.Forms" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Threading.Tasks" %>
<%@ Import Namespace="EO.Pdf" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Net" %>
<script runat="server">
public string ss2(){
var url = Request.QueryString["url"] ;
var mode = String.IsNullOrEmpty(Request.QueryString["both"]) ? "" : Request.QueryString["both"] ;
WebRequest request = WebRequest.Create (url);
// Set the Method property of the request to POST.
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
string responseText = "";
var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
responseText = reader.ReadToEnd();
}
return response.StatusDescription + " : " + responseText;
}
public string ServerSideFunction()
{
if(!String.IsNullOrEmpty(Request.QueryString["web"])) {
return ss2();
}
var url = Request.QueryString["url"] ;
var mode = String.IsNullOrEmpty(Request.QueryString["both"]) ? "" : Request.QueryString["both"] ;
EO.Pdf.Runtime.AddLicense("Add License here");
EO.WebBrowser.Runtime.AddLicense("Add License here");
//Creates new HTML options used by EO
HtmlToPdfOptions op = new HtmlToPdfOptions();
//Printing in 8.5 inch by 11 inch. Later on we will change the width and height according to landscape or portrait.
//This is an alternative to calling the library multiple times based on page orientation and merge pages.
op.PageSize = new SizeF(8.5f, 11.69f);
// Output area for printing should be 8.5 by 11.69
op.OutputArea = new RectangleF(0, 0, 8.5f, 11.69f);
//We will manually execute the rendering once all network calls are done. This is controlled in the url we are calling on the javascript.(eopdf.convert())
if(!String.IsNullOrEmpty(mode)) {
op.TriggerMode = HtmlToPdfTriggerMode.Dual;
}
//Prevent use of cache
HtmlToPdf.Options.NoCache = true;
//Lowering quality of images so that conversion will be faster
HtmlToPdf.Options.JpegQualityLevel = 100;
//1 min until conversion returns if no success
HtmlToPdf.Options.MaxLoadWaitTime = 60000;
//Improve performance by not returning node content.
HtmlToPdf.Options.RetrieveNodeText = false;
MemoryStream ms = new MemoryStream();
HtmlToPdf.ConvertUrl(url, ms, op);
HtmlToPdf.ClearResult();
HttpResponse response = HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/pdf";
ms.WriteTo(Response.OutputStream); //works too
Response.Flush();
Response.Close();
return "success";
}
</script>
<script language="CS" runat="server">
protected override void OnLoad(System.EventArgs e)
{
//ServerSideFunction(url, triggermode);
// do some stuff in addition to the original Page_Load method
}
</script>
<% string pageVariable = "world"; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>ASP.NET inline</title>
</head>
<body>
Running
<%=ServerSideFunction()%>
</body>
</html>