10

ドキュメントをネットワークプリンター(\ myserver \ myprinter)に送信する必要があります。System.Printingクラスを使用して印刷していますが、Windowsサービスからの場合は正常に機能しますが、ASP.NETアプリからは、ネットワークプリンターではなく、ローカルプリンターにのみ印刷できます。私が得ているエラーは「プリンター名が無効です」ですこれは私がプリンター名を取得するために使用しているものです:

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

ここでの私のオプションは何ですか?これは権限の問題ですか?

4

3 に答える 3

5

既定では、ASP.NETアプリケーションは、制限された権限を持つ特別なアカウントで実行されます。Webページを提供するのに十分であり、それ以上のものはありません。したがって、ASPNETユーザーを構成する必要があります。

対照的に、Windowsサービスは通常ローカルシステムアカウントで実行されます(高い特権で)

于 2010-09-16T17:36:23.870 に答える
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()
于 2010-09-16T17:43:38.923 に答える
1

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;

最初に呼び出しを行ってユーザーになりすまし、次に次のようなprint関数を呼び出します。

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

于 2016-05-14T12:21:39.420 に答える