我需要将文档发送到网络打印机(\myserver\myprinter)。我使用System.Printing类进行打印,当它来自Windows Service时,它工作得很好,但是从ASP.NET应用程序,它只能打印到本地打印机,而不能打印到网络打印机。我得到的错误是“打印机名称无效”,这是我用来获取打印机名称的代码:
public string PrinterName
{
using (LocalPrintServer server = new LocalPrintServer())
return server.GetPrintQueue(@"\\myserver\myprinter");
}我在这里有什么选择?这是权限问题吗?
发布于 2010-09-17 01:43:39
凭据存在一些问题,您可以通过模拟或提升运行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()发布于 2010-09-17 01:36:24
默认情况下,ASP.NET应用程序在具有有限权限的特殊帐户上运行。仅够提供网页服务,仅此而已。因此,您必须配置ASPNET用户。
相比之下,Windows服务通常在本地系统帐户下运行(具有高权限)
发布于 2016-05-14 20:21:39
可以使用以下命令从ASP.Net/C#完成网络打印:
如果为域用户配置了网络,并将打印机添加到打印服务器:
要定义的打印机访问PrinterName= "\PrintServerIP_OR_Name\PRINTERNAME“示例: PrinterSettings.PrinterName = "\15.1.1.1\prn001"
/// <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();
}
https://stackoverflow.com/questions/3729153
复制相似问题