從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