从ASP.NET打印到网络打印机
我需要将文档发送到网络打印机(\ myserver \ myprinter)。我使用System.Printing类进行打印,当它来自Windows服务时,它可以正常工作,但是从ASP.NET应用程序只能打印到本地打印机而不是网络打印机。我得到的错误是“打印机名称是无效的”这是我用得到打印机的名称是什么:从ASP.NET打印到网络打印机
public string PrinterName
{
using (LocalPrintServer server = new LocalPrintServer())
return server.GetPrintQueue(@"\\myserver\myprinter");
}
什么是我选择这里?这是一个权限问题?
有与您可以通过假冒或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()
那么我可以使用\\ myserver \ myprinter作为PrinterName吗?我们遗漏了\\ myserver \ – Prabhu 2010-09-16 17:53:43
。无论我们在服务器上如何命名,还有另一个词是我们如何称呼它的。没有UNC路径或任何东西。 – klabranche 2010-09-16 17:55:24
噢好吧,你是说你将网络打印机安装为本地打印机? – Prabhu 2010-09-16 17:57:32
从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();
}
在哪个用户上下文ASP.NET运行?你在使用模拟吗?打印机的权限是什么? – Heinzi 2010-09-16 17:32:01
它在ASP.NET Development Server中运行,所以我假设它在我的Windows帐户下运行。我可以直接从记事本打印到该服务器打印机。 – Prabhu 2010-09-16 20:17:54