从 ASP.NET 打印到网络打印机

发布于 2024-09-24 05:15:30 字数 366 浏览 3 评论 0原文

我需要将文档发送到网络打印机 (\myserver\myprinter)。我使用 System.Printing 类进行打印,当它来自 Windows 服务时它工作正常,但来自 ASP.NET 应用程序时,它只能打印到本地打印机,而不能打印到网络打印机。我收到的错误是“打印机名称无效”这是我用来获取打印机名称的内容:

public string PrinterName
{
   using (LocalPrintServer server = new LocalPrintServer())
   return server.GetPrintQueue(@"\\myserver\myprinter");
}

我在这里有哪些选项?这是权限问题吗?

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, but from an ASP.NET app, it's only able to print to local printers, not network printers. The error I'm getting is "Printer Name is not valid" This is what I'm using to get the printer name:

public string PrinterName
{
   using (LocalPrintServer server = new LocalPrintServer())
   return server.GetPrintQueue(@"\\myserver\myprinter");
}

What are my options here? Is this a permissions problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑叹一世浮沉 2024-10-01 05:15:30

您可以通过模拟或提升运行 Web 应用程序的用户的权限来解决凭据问题。

但是,我们通过将网络打印机添加为服务器上的打印机(在服务器上添加打印机对话)并将作业发送到该打印机来做到这一点。

我们像这样使用 Printing.PrintDocument (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()

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()
离去的眼神 2024-10-01 05:15:30

默认情况下,ASP.NET 应用程序在具有有限权限的特殊帐户上运行。足以提供网页服务,仅此而已。因此,您必须配置 ASPNET 用户。

相比之下,Windows 服务通常在本地系统帐户下运行(具有高权限)

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)

满栀 2024-10-01 05:15:30

ASP.Net/C# 的网络打印可以使用以下方法完成:

如果为域用户配置网络并且将打印机添加到打印服务器:

  • PrinterName 定义为 = "\\PrintServerIP_OR_Name\\PRINTERNAME"
    示例: PrinterSettings.PrinterName = "\\15.1.1.1\\prn001"
  • 检查打印机访问上设置的权限
  • ,是域用户还是所有人
  • 如果是域用户,则可以将 C# 代码包含在可用于的模拟中调用打印代码如下:
/// <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;

首先调用模拟用户,然后调用打印函数,如下所示:

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

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();
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文