Printing from ASP.NET to a network printer

前端 未结 3 1408
醉酒成梦
醉酒成梦 2020-12-01 11:31

I need to send documents to a network printer (\\myserver\\myprinter). I\'m using the System.Printing classes to print, and it works fine when it\'s from a Windows Service,

相关标签:
3条回答
  • 2020-12-01 11:52

    There are issues with credentials that you could solve by impersonation or elevating rights of the user the web app is running under.

    However, we did it by adding the network printer as a printer on the server (add printer dialogue on server) and having the job sent to that printer.

    We used the Printing.PrintDocument like so (Code in VB)....

    Public Class SpecialReportPrintJob
      Inherits Printing.PrintDocument
    
    Protected Overrides Sub OnBeginPrint(ByVal ev as Printing.PrintEventArgs)
      MyBase.OnBeginPrint(ev)
    
      Me.PrinterSettings.PrinterName = "PrinterNameUsedOnServer"
    
      'setup rest of stuff....
    End Sub  
    End Class
    'And we then call it like so
    Dim printSpecialReport as new SpecialReportPrintJob()
    printSpecialReport.Print()
    
    0 讨论(0)
  • 2020-12-01 12:03

    The Network Printing from ASP.Net/C# can be done using:

    If the Network is configured for Domain Users and Printer is added to print server:

    • PrinterName to be defined as = "\\PrintServerIP_OR_Name\\PRINTERNAME" Example: PrinterSettings.PrinterName = "\\15.1.1.1\\prn001"
    • Check the permission set on the Printer Access
    • Which either be Domain Users or Everyone
    • If Domain Users, then the C# code can be enclosed within the impersonation that can be used to call the print code which is as below:
    /// <summary>
        /// Does the actual impersonation.
        /// </summary>
        /// <param name="userName">The name of the user to act as.</param>
        /// <param name="domainName">The domain name of the user to act as.</param>
        /// <param name="password">The password of the user to act as.</param>
        private void ImpersonateValidUser(
            string userName, 
            string domain, 
            string password )
        {
            WindowsIdentity tempWindowsIdentity = null;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;
    
            try
            {
                if ( RevertToSelf() )
                {
                    if ( LogonUser(
                        userName, 
                        domain, 
                        password, 
                        LOGON32_LOGON_INTERACTIVE,
                        LOGON32_PROVIDER_DEFAULT, 
                        ref token ) != 0 )
                    {
                        if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
                        {
                            tempWindowsIdentity = new WindowsIdentity( tokenDuplicate );
                            impersonationContext = tempWindowsIdentity.Impersonate();
                        }
                        else
                        {
                            throw new Win32Exception( Marshal.GetLastWin32Error() );
                        }
                    }
                    else
                    {
                        throw new Win32Exception( Marshal.GetLastWin32Error() );
                    }
                }
                else
                {
                    throw new Win32Exception( Marshal.GetLastWin32Error() );
                }
            }
            finally
            {
                if ( token!= IntPtr.Zero )
                {
                    CloseHandle( token );
                }
                if ( tokenDuplicate!=IntPtr.Zero )
                {
                    CloseHandle( tokenDuplicate );
                }
            }
        }
    
        /// <summary>
        /// Reverts the impersonation.
        /// </summary>
        private void UndoImpersonation()
        {
            if ( impersonationContext!=null )
            {
                impersonationContext.Undo();
            }   
        }
    
        private WindowsImpersonationContext impersonationContext = null;
    

    First make an call to impersonate the user and then call the print function that would look like below:

    if(ImpersonateValidUser("username", "domain", "password"))
    {
      PrintDetails();
      UndoImpersonation();
    }

    0 讨论(0)
  • 2020-12-01 12:08

    By default, an ASP.NET application runs on a special account with limited rights. Just enough to serve webpages, nothing more. So you'll have to configure the ASPNET user.

    By contrast Windows services usually run under local System account (with high privileges)

    0 讨论(0)
提交回复
热议问题