从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