Windows 窗体
在 Windows 窗体项目中,您可以传递合适的System.Net.NetworkCredential https://msdn.microsoft.com/en-us/library/system.net.networkcredential(v=vs.110).aspx to ServerReport.ReportServerCredentials.NetworkCredentials https://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportservercredentials.networkcredentials(v=vs.100).aspx的财产ReportViewer
。这样,所有报告都将使用传递的凭据执行:
reportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials =
new System.Net.NetworkCredential("username", "password", "domain");
网页表格
Web 表单的解决方案有所不同。在 Web 表单项目中,将合适的凭据传递给RePortViewer
你需要实施IReportServerCredentials https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.ireportservercredentials.aspx。然后您可以将值分配给ServerReport.ReportServerCredentials https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.serverreport.reportservercredentials.aspx的财产ReportViewer
控制。这样,所有报告都将使用传递的凭据执行。
Example
这是一个简单的实现。最好将用户名、密码和域名存储在应用程序配置中并从配置文件中读取它们:
using System;
using System.Net;
using System.Security.Principal;
using Microsoft.Reporting.WebForms;
[Serializable]
public sealed class MyReportServerCredentials : IReportServerCredentials
{
public WindowsIdentity ImpersonationUser { get { return null; } }
public ICredentials NetworkCredentials
{
get
{
return new NetworkCredential("username", "password", "domain");
}
}
public bool GetFormsCredentials(out Cookie authCookie, out string userName,
out string password, out string authority)
{
authCookie = null;
userName = password = authority = null;
return false;
}
}
Then in Page_Load
通过这种方式传递凭证:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
this.ReportViewer1.ServerReport.ReportServerCredentials =
new Sample.MyReportServerCredentials();
}
Note
如果您想使用ReportViewer
没有会话状态,你也可以实现IReportServerConnection https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.ireportserverconnection.aspx。在这种情况下,您需要添加一个键值appsettings
配置文件的部分以这种方式介绍实现:
<add key="ReportViewerServerConnection" value="YourNameSpace.YourClass, YourAssemply" />
在这种情况下,您不需要代码Page_Load
并且配置就足够了。欲了解更多信息,请查看this https://blogs.msdn.microsoft.com/brianhartman/2008/11/21/custom-credentials-in-the-report-viewer/布莱恩·哈特曼 (Brian Hartman) 撰写的精彩博客文章。