How to use WebClientPrint with ASP.NET Authentication enabled
Product WebClientPrint for ASP.NET Published 08/22/2013 Updated 12/09/2016 Author Neodynamic
Overview
WebClientPrint is a client-server solution which means that it provides a server-side component (Neodynamic.SDK.WebClientPrint.dll) and a client-side software Windows, Linux, Raspberry Pi & Mac called WCPP (WebClientPrint Processor)
Both parts of WebClientPrint solution communicates through HTTP/HTTPS protocol. When the client-side utility (WCPP) connects to your website requesting a ClientPrintJob, it expects to get such object back to it. If your website requires user authentication, then when WCPP tries to get a ClientPrintJob from your site, it'll fail and an error message will be displayed to the user (usually a 401 Unauthorized HTTP Code is displayed or WCPP is redirected to a kind of LOGIN page). You must know that WCPP is unable to provide any user credentials for authentication so to get it working within a website with ASP.NET Authentication enabled, a couple of changes must be done.
In this walkthrough, you'll learn how to structure your code to get WebClientPrint working with ASP.NET Authentication feature enabled.
WebClientPrint 3.0+ Instructions
For ASP.NET Forms or Windows Authentication
If your site is configured with Forms or Windows Authentication, then do this:
- Ensure the AllowAnonymous attribute is applied to the ProcessRequest() method of WebClientPrintAPIController:
public class WebClientPrintAPIController : Controller { [AllowAnonymous] public void ProcessRequest() { //... //CODE REMOVED FOR SIMPLICITY!!! //... } }
- Add the AllowAnonymous attribute to the Controller's Action that creates the ClientPrintJob. For example:
public class PrintPDFController : Controller { [AllowAnonymous] public void PrintFile(string useDefaultPrinter, string printerName) { //... //CODE REMOVED FOR SIMPLICITY!!! //... } }
That's it! Now WebClientPrint solution should work fine in your website with ASP.NET Authentication enabled.
For ASP.NET Forms or Windows Authentication
If your site is configured with Forms or Windows Authentication, then do this:
- Ensure the AllowAnonymous attribute is applied to the ProcessRequest() method of WebClientPrintAPIController:
Imports Neodynamic.SDK.Web Namespace Controllers Public Class WebClientPrintAPIController Inherits Controller <AllowAnonymous> Public Sub ProcessRequest() '... 'CODE REMOVED FOR SIMPLICITY!!! '... End Sub End Class End Namespace
- Add the AllowAnonymous attribute to the Controller's Action that creates the ClientPrintJob. For example:
Imports Neodynamic.SDK.Web Namespace Controllers Public Class PrintPDFController Inherits Controller <AllowAnonymous> Public Sub PrintFile(useDefaultPrinter As String, printerName As String) '... 'CODE REMOVED FOR SIMPLICITY!!! '... End Sub End Class End Namespace
That's it! Now WebClientPrint solution should work fine in your website with ASP.NET Authentication enabled.
For ASP.NET Forms Authentication
If your site is configured with Forms Authentication, the generic handler called WebClientPrintAPI.ashx as well as ANY othe Generic Handler *.ASHX referencing WebClientPrint classes like ClientPrintJob must be set to ALLOW ANONYMOUS access in the web.config. Here's an example... Open your web.config file and add these new entries:
<location path="WebClientPrintAPI.ashx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<system.webServer>
<security>
<authorization>
<add accessType="Allow" users="*"/>
</authorization>
</security>
</system.webServer>
</location>
<location path="PrintPDFHandler.ashx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<system.webServer>
<security>
<authorization>
<add accessType="Allow" users="*"/>
</authorization>
</security>
</system.webServer>
</location>
That's it! Now WebClientPrint solution should work fine in your website with ASP.NET Forms Authentication enabled.
For Windows Authentication
If your site is configured with Windows Authentication, the generic handler called WebClientPrintAPI.ashx as well as ANY othe Generic Handler *.ASHX referencing WebClientPrint classes like ClientPrintJob must be set to ALLOW ANONYMOUS access in IIS Manager. Follow up these steps:
Open Internet Information Services (IIS) Manager, select your website and do the following:
- Enable Windows authentication to the root site.
- Select the WebClientPrintAPI.ashx file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
- Repeat previous step with ANY othe Generic Handler *.ASHX referencing WebClientPrint classes like ClientPrintJob e.g. Select the PrintPDFHandler.ashx file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
That's it! Now WebClientPrint solution should work fine in your website with Windows Authentication enabled.
For ASP.NET Forms Authentication
If your site is configured with Forms Authentication, the generic handler called WebClientPrintAPI.ashx as well as ANY othe Generic Handler *.ASHX referencing WebClientPrint classes like ClientPrintJob must be set to ALLOW ANONYMOUS access in the web.config. Here's an example... Open your web.config file and add these new entries:
<location path="WebClientPrintAPI.ashx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<system.webServer>
<security>
<authorization>
<add accessType="Allow" users="*"/>
</authorization>
</security>
</system.webServer>
</location>
<location path="PrintPDFHandler.ashx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<system.webServer>
<security>
<authorization>
<add accessType="Allow" users="*"/>
</authorization>
</security>
</system.webServer>
</location>
That's it! Now WebClientPrint solution should work fine in your website with ASP.NET Forms Authentication enabled.
For Windows Authentication
If your site is configured with Windows Authentication, the generic handler called WebClientPrintAPI.ashx as well as ANY othe Generic Handler *.ASHX referencing WebClientPrint classes like ClientPrintJob must be set to ALLOW ANONYMOUS access in IIS Manager. Follow up these steps:
Open Internet Information Services (IIS) Manager, select your website and do the following:
- Enable Windows authentication to the root site.
- Select the WebClientPrintAPI.ashx file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
- Repeat previous step with ANY othe Generic Handler *.ASHX referencing WebClientPrint classes like ClientPrintJob e.g. Select the PrintPDFHandler.ashx file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
That's it! Now WebClientPrint solution should work fine in your website with Windows Authentication enabled.
WebClientPrint 2.0 Instructions - A Sample Scenario
Suppose you have a Controller called PrintReportController with an Action called PrintSalesReport where a ClientPrintJob object is created and returned to the WCPP when required. In ASP.NET MVC, the code for such page might look like follows:
VB
Public Sub PrintSalesReport()
Dim file As New PrintFile("c:\myReport.pdf", "myReport.pdf")
Dim cpj As New ClientPrintJob()
cpj.PrintFile = file
cpj.ClientPrinter = New DefaultPrinter()
cpj.SendToClient(System.Web.HttpContext.Current.Response)
End Sub
C#
public void PrintSalesReport()
{
PrintFile file = new PrintFile(@"c:\myReport.pdf", "myReport.pdf");
ClientPrintJob cpj = new ClientPrintJob();
cpj.PrintFile = file;
cpj.ClientPrinter = new DefaultPrinter();
cpj.SendToClient(System.Web.HttpContext.Current.Response);
}
The ASP.NET/HTML Razor markup of the View should have the following code too:
@* Register the WebClientPrint script code. The param is the URL for the PrintSalesReport action method in the PrintReportController. *@
@Html.Raw(Neodynamic.SDK.Web.WebClientPrint.CreateScript(Url.Action("PrintSalesReport", "PrintReport", null, Request.Url.Scheme)))
Suppose you have a page called PrintReport.aspx where a ClientPrintJob object is created and returned to the WCPP when required. In ASP.NET WebForms, the code for such page might look like follows:
VB
Protected Sub Page_Init(sender As Object, e As System.EventArgs)
'Print report file?
If (WebClientPrint.ProcessPrintJob(Request)) Then
'report file
Dim pdfFilePath As String = "c:\myReport.pdf"
'Create a PrintFile object with the PDF file
Dim file As New PrintFile(pdfFilePath, "myReport.pdf")
'Create a ClientPrintJob and send it back to the client!
Dim cpj As New ClientPrintJob()
'set file to print...
cpj.PrintFile = file
'set client printer...
cpj.ClientPrinter = New DefaultPrinter()
'send it...
cpj.SendToClient(Response)
End If
End Sub
C#
protected void Page_Init(object sender, EventArgs e)
{
//Print report file?
if (WebClientPrint.ProcessPrintJob(Request))
{
//report file
string pdfFilePath = @"c:\myDocument.pdf";
//Create a PrintFile object with the PDF file
PrintFile file = new PrintFile(pdfFilePath, "MyReport.pdf");
//Create a ClientPrintJob and send it back to the client!
ClientPrintJob cpj = new ClientPrintJob();
//set file to print...
cpj.PrintFile = file;
//set client printer...
cpj.ClientPrinter = new DefaultPrinter();
//send it...
cpj.SendToClient(Response);
}
}
The ASP.NET/HTML markup of this page should have the following code too:
<%-- Register the WebClientPrint script code --%>
<%=Neodynamic.SDK.Web.WebClientPrint.CreateScript()%>
Now, if ASP.NET Authentication is enabled, then the above code will not work because once again, the WCPP cannot provide any user credentials while requesting the ClientPrintJob object.
Solutions when ASP.NET Authentication is enabled
For ASP.NET Forms Authentication
If your site is configured with Forms Authentication, then do this:
-
Just add the AllowAnonymous attribute to the Controller's Action that creates the ClientPrintJob. In our sample, it should look like this now:
VB
<AllowAnonymous()> Public Sub PrintSalesReport() Dim file As New PrintFile("c:\myReport.pdf", "myReport.pdf") Dim cpj As New ClientPrintJob() cpj.PrintFile = file cpj.ClientPrinter = New DefaultPrinter() cpj.SendToClient(System.Web.HttpContext.Current.Response) End Sub
C#
[AllowAnonymous] public void PrintSalesReport() { PrintFile file = new PrintFile(@"c:\myReport.pdf", "myReport.pdf"); ClientPrintJob cpj = new ClientPrintJob(); cpj.PrintFile = file; cpj.ClientPrinter = new DefaultPrinter(); cpj.SendToClient(System.Web.HttpContext.Current.Response); }
-
Open your web.config file and add these new entries:
<location path="wcp.axd"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> <system.webServer> <security> <authorization> <add accessType="Allow" users="*"/> </authorization> </security> </system.webServer> </location>
The above entries is to make publicly accessible the code involved with the WebClientPrint solution. That's it! Now WebClientPrint should work even if your site has ASP.NET Forms Authentication enabled.
- Create a new Generic Handler file at your website root and name it PrintMyReport.ashx
-
Move the code you have under Page_Init event handler of PrintReport.aspx to the ProcessRequest method of the PrintMyReport.ashx file
NOTE: Minor changes must be done when moving your code as e.g. in the aspx page you reference Response object directly while in the ashx file you have to reference it as context.ResponseThe new code for ProcessRequest should look like this now:
VB
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 'Print report file? If (WebClientPrint.ProcessPrintJob(context.Request)) Then 'report file Dim pdfFilePath As String = "c:\myReport.pdf" 'Create a PrintFile object with the PDF file Dim file As New PrintFile(pdfFilePath, "myReport.pdf") 'Create a ClientPrintJob and send it back to the client! Dim cpj As New ClientPrintJob() 'set file to print... cpj.PrintFile = file 'set client printer... cpj.ClientPrinter = New DefaultPrinter() 'send it... cpj.SendToClient(context.Response) End If End Sub
C#
public void ProcessRequest (HttpContext context) { //Print report file? if (WebClientPrint.ProcessPrintJob(context.Request)) { //report file string pdfFilePath = @"c:\myReport.pdf"; //Create a PrintFile object with the PDF file PrintFile file = new PrintFile(pdfFilePath, "MyReport.pdf"); //Create a ClientPrintJob and send it back to the client! ClientPrintJob cpj = new ClientPrintJob(); //set file to print... cpj.PrintFile = file; //set client printer... cpj.ClientPrinter = new DefaultPrinter(); //send it... cpj.SendToClient(context.Response); } }
-
Add a new class to App_Code folder naming it as MyUtils and paste the following code:
VB
Imports System.Web Public Class MyUtils Public Shared Function GetWebsiteRoot() As String Dim req As HttpRequest = HttpContext.Current.Request Dim port As String = IIf(req.Url.Port = 80 OrElse req.Url.Port = 443, "", ":" + req.Url.Port.ToString()) Return req.Url.Scheme + "://" + req.Url.Host + port + req.ApplicationPath End Function End Class
C#
using System; using System.Web; public class MyUtils { public static string GetWebsiteRoot() { HttpRequest req = HttpContext.Current.Request; string port = (req.Url.Port == 80 || req.Url.Port == 443 ? "" : ":" + req.Url.Port.ToString()); return req.Url.Scheme + "://" + req.Url.Host + port + req.ApplicationPath; } }
-
Open your PrintReport.aspx page and change the code so it looks like this:
<%-- Register the WebClientPrint script code --%> <%=Neodynamic.SDK.Web.WebClientPrint.CreateScript(MyUtils.GetWebsiteRoot() + "/PrintMyReport.ashx")%>
-
Open your web.config file and add these new entries:
<location path="wcp.axd"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> <system.webServer> <security> <authorization> <add accessType="Allow" users="*"/> </authorization> </security> </system.webServer> </location> <location path="PrintMyReport.ashx"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> <system.webServer> <security> <authorization> <add accessType="Allow" users="*"/> </authorization> </security> </system.webServer> </location>
The above entries is to make publicly accessible the code involved with the WebClientPrint solution. That's it! Now WebClientPrint should work even if your site has ASP.NET Forms Authentication enabled.
For ASP.NET Windows Authentication
If your site is configured with Windows Authentication, then do this:
-
Just add the AllowAnonymous attribute to the Controller's Action that creates the ClientPrintJob. In our sample, it should look like this now:
VB
<AllowAnonymous()> Public Sub PrintSalesReport() Dim file As New PrintFile("c:\myReport.pdf", "myReport.pdf") Dim cpj As New ClientPrintJob() cpj.PrintFile = file cpj.ClientPrinter = New DefaultPrinter() cpj.SendToClient(System.Web.HttpContext.Current.Response) End Sub
C#
[AllowAnonymous] public void PrintSalesReport() { PrintFile file = new PrintFile(@"c:\myReport.pdf", "myReport.pdf"); ClientPrintJob cpj = new ClientPrintJob(); cpj.PrintFile = file; cpj.ClientPrinter = new DefaultPrinter(); cpj.SendToClient(System.Web.HttpContext.Current.Response); }
- At the website root, create an empty text file and name it as wcp.axd
- Save your ASP.NET project.
-
Open Internet Information Services (IIS) Manager, select your website and do the following:
- Enable Windows authentication to the root site.
- Select the wcp.axd file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
That's it. Now WebClientPrint should work even if your site has ASP.NET Windows Authentication enabled.
- Create a new Generic Handler file at your website root and name it PrintMyReport.ashx
-
Move the code you have under Page_Init event handler of PrintReport.aspx to the ProcessRequest method of the PrintMyReport.ashx file
NOTE: Minor changes must be done when moving your code as e.g. in the aspx page you reference Response object directly while in the ashx file you have to reference it as context.ResponseThe new code for ProcessRequest should look like this now:
VB
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 'Print report file? If (WebClientPrint.ProcessPrintJob(context.Request)) Then 'report file Dim pdfFilePath As String = "c:\myReport.pdf" 'Create a PrintFile object with the PDF file Dim file As New PrintFile(pdfFilePath, "myReport.pdf") 'Create a ClientPrintJob and send it back to the client! Dim cpj As New ClientPrintJob() 'set file to print... cpj.PrintFile = file 'set client printer... cpj.ClientPrinter = New DefaultPrinter() 'send it... cpj.SendToClient(context.Response) End If End Sub
C#
public void ProcessRequest (HttpContext context) { //Print report file? if (WebClientPrint.ProcessPrintJob(context.Request)) { //report file string pdfFilePath = @"c:\myReport.pdf"; //Create a PrintFile object with the PDF file PrintFile file = new PrintFile(pdfFilePath, "MyReport.pdf"); //Create a ClientPrintJob and send it back to the client! ClientPrintJob cpj = new ClientPrintJob(); //set file to print... cpj.PrintFile = file; //set client printer... cpj.ClientPrinter = new DefaultPrinter(); //send it... cpj.SendToClient(context.Response); } }
-
Add a new class to App_Code folder naming it as MyUtils and paste the following code:
VB
Imports System.Web Public Class MyUtils Public Shared Function GetWebsiteRoot() As String Dim req As HttpRequest = HttpContext.Current.Request Dim port As String = IIf(req.Url.Port = 80 OrElse req.Url.Port = 443, "", ":" + req.Url.Port.ToString()) Return req.Url.Scheme + "://" + req.Url.Host + port + req.ApplicationPath End Function End Class
C#
using System; using System.Web; public class MyUtils { public static string GetWebsiteRoot() { HttpRequest req = HttpContext.Current.Request; string port = (req.Url.Port == 80 || req.Url.Port == 443 ? "" : ":" + req.Url.Port.ToString()); return req.Url.Scheme + "://" + req.Url.Host + port + req.ApplicationPath; } }
-
Open your PrintReport.aspx page and change the code so it looks like this:
<%-- Register the WebClientPrint script code --%> <%=Neodynamic.SDK.Web.WebClientPrint.CreateScript(MyUtils.GetWebsiteRoot() + "/PrintMyReport.ashx")%>
- At the website root, create an empty text file and name it as wcp.axd
- Save your ASP.NET project.
-
Open Internet Information Services (IIS) Manager, select your website and do the following:
- Enable Windows authentication to the root site.
- Select the PrintMyReport.ashx file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
- Select the wcp.axd file and DISABLE Windows authentication BUT ENABLE Anonymous Authentication
That's it. Now WebClientPrint should work even if your site has ASP.NET Windows Authentication enabled.