我在 SQL2005 报告服务器上保存了一份报告,我想返回该报告的渲染 PDF。我在使用本地 *.rdlc 文件时发现了这一点(我已经在博客上介绍过它 http://www.jarrettmeyer.com/2009/09/reports-in-aspnet-mvc.html),但当 *.rdl 驻留在报告服务器上时则不然。我得到了401 未授权线路错误...
reportViewer.ServerReport.SetParameters(reportDefinition.ReportParameters);
这是用于呈现报告的方法。
public byte[] Render(IReportDefinition reportDefinition)
{
var reportViewer = new ReportViewer();
byte[] renderedReport;
try
{
var credentials = new WindowsImpersonationCredentials();
reportViewer.ServerReport.ReportServerUrl = new Uri("http://myssrsbox", UrlKind.Absolute);
reportViewer.ServerReport.ReportServerCredentials = credentials;
reportViewer.ServerReport.ReportPath = reportDefinition.Path;
// Exception is thrown on the following line...
reportViewer.ServerReport.SetParameters(reportDefinition.ReportParameters);
string mimeType;
string encoding;
string filenameExtension;
string[] streams;
Warning[] warnings;
renderedReport = reportViewer.ServerReport.Render(reportDefinition.OutputType, reportDefinition.DeviceInfo, out mimeType, out encoding, out filenameExtension, out streams, out warnings);
}
catch (Exception ex)
{
// log the error...
throw;
}
finally
{
reportViewer.Dispose();
}
return renderedReport;
}
您缺少的另一件事是 Windows Impersonation Credentials 类。
public class WindowsImpersonationCredentials : IReportServerCredentials
{
public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = password = authority = null;
return false;
}
public WindowsIdentity ImpersonationUser
{
get { return WindowsIdentity.GetCurrent(); }
}
public ICredentials NetworkCredentials
{
get { return null; }
}
public override string ToString()
{
return String.Format("WindowsIdentity: {0} ({1})", this.ImpersonationUser.Name, this.ImpersonationUser.User.Value);
}
}
您可能需要了解的其他事项...
- 它正在 Intranet 上运行,并且模拟已打开。
- 日志记录表明模拟用户设置正确。
- This 确实有效在 Visual Studio 中运行时(
http://localhost:devport
),并且它确实有效在我的开发盒上运行时(http://localhost/myApplication
). It 不起作用在我们的测试或生产服务器上运行时。
- 我尝试过在 web.config 中使用和不使用 system.net.defaultProxy 设置的解决方案。两者都不起作用。
我究竟做错了什么?是服务器设置吗?是代码吗?是 web.config 吗?