2010-09-16 360 views
10

我需要將文檔發送到網絡打印機(\ myserver \ myprinter)。我使用System.Printing類進行打印,當它來自Windows服務時,它可以正常工作,但是從ASP.NET應用程序只能打印到本地打印機而不是網絡打印機。我得到的錯誤是「打印機名稱是無效的」這是我用得到打印機的名稱是什麼:從ASP.NET打印到網絡打印機

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

什麼是我選擇這裏?這是一個權限問題?

+2

在哪個用戶上下文ASP.NET運行?你在使用模擬嗎?打印機的權限是什麼? – Heinzi 2010-09-16 17:32:01

+0

它在ASP.NET Development Server中運行,所以我假設它在我的Windows帳戶下運行。我可以直接從記事本打印到該服務器打印機。 – Prabhu 2010-09-16 20:17:54

回答

5

有與您可以通過假冒或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() 
+0

那麼我可以使用\\ myserver \ myprinter作爲PrinterName嗎?我們遺漏了\\ myserver \ – Prabhu 2010-09-16 17:53:43

+0

。無論我們在服務器上如何命名,還有另一個詞是我們如何稱呼它的。沒有UNC路徑或任何東西。 – klabranche 2010-09-16 17:55:24

+0

噢好吧,你是說你將網絡打印機安裝爲本地打印機? – Prabhu 2010-09-16 17:57:32

5

默認情況下,ASP.NET應用程序在具有有限權限的特殊帳戶上運行。僅僅爲網頁提供服務,僅此而已。所以你必須配置ASPNET用戶。

相反的Windows服務,通常在本地系統帳戶運行(高權限)

+0

謝謝...你知道我怎麼配置它給它足夠的特權? – Prabhu 2010-09-16 17:45:05

+0

嗨,我是一名程序員,而不是SysOp。問[在那裏](http://serverfault.com/) – 2010-09-16 17:47:26

+0

謝謝。我檢查了它也不適用於Windows窗體應用程序。 – Prabhu 2010-09-16 17:56:09

0

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