首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ASP.NET打印到网络打印机

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

Stack Overflow用户
提问于 2010-09-17 01:30:03
回答 3查看 34.8K关注 0票数 10

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

代码语言:javascript
复制
public string PrinterName
{
   using (LocalPrintServer server = new LocalPrintServer())
   return server.GetPrintQueue(@"\\myserver\myprinter");
}

我在这里有什么选择?这是权限问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-17 01:43:39

凭据存在一些问题,您可以通过模拟或提升运行web应用程序的用户的权限来解决这些问题。

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

我们这样使用Printing.PrintDocument (VB中的代码)....

代码语言:javascript
复制
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()
票数 5
EN

Stack Overflow用户

发布于 2010-09-17 01:36:24

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

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

票数 5
EN

Stack Overflow用户

发布于 2016-05-14 20:21:39

可以使用以下命令从ASP.Net/C#完成网络打印:

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

要定义的打印机访问PrinterName= "\PrintServerIP_OR_Name\PRINTERNAME“示例: PrinterSettings.PrinterName = "\15.1.1.1\prn001"

  • Check为域用户或Everyone

  • 的打印机访问

  • 上设置的权限。如果是域用户,则可以将C#代码包含在可用于调用打印代码的模拟中,如下所示:

代码语言:javascript
复制
/// <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;

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

代码语言:javascript
复制
if(ImpersonateValidUser("username", "domain", "password"))
{
  PrintDetails();
  UndoImpersonation();
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3729153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档