我正在尝试通过 ASP.NET Web ReportViewer 控件连接到 ReportingServices:
rvContract.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
rvContract.ServerReport.ReportServerCredentials = new ReportServerCredentials("myUsername", "myNetworkPassword", "DOMAIN");
rvContract.ServerReport.ReportServerUrl = new Uri(ReportConfiguration.ReportServerUrl);
string rptPath = ReportConfiguration.RootPath;
if (!rptPath.EndsWith("/"))
{
rptPath += "/";
}
rvContract.ServerReport.ReportPath = rptPath + "AdminReports/Contract";
List<ReportParameter> reportParams = new List<ReportParameter>();
if (MkoSession.AccountId.HasValue)
{
ReportParameter accountId = new ReportParameter("AccountId", MkoSession.AccountId.Value.ToString());
}
rvContract.ServerReport.SetParameters(reportParams);
rvContract.ShowParameterPrompts = false;
rvContract.ShowZoomControl = false;
rvContract.ServerReport.Refresh();
rvContract.DataBind();
凭证的实现如下所示:
public class ReportServerCredentials : IReportServerCredentials
{
private string _userName;
private string _password;
private string _domain;
public ReportServerCredentials(string userName, string password, string domain)
{
_userName = userName;
_password = password;
_domain = domain;
}
public WindowsIdentity ImpersonationUser
{
get
{
// Use default identity.
return null;
}
}
public ICredentials NetworkCredentials
{
get
{
// Use default identity.
return new NetworkCredential(_userName, _password, _domain);
}
}
public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
{
// Do not use forms credentials to authenticate.
authCookie = null;
user = null;
password = null;
authority = null;
return false;
}
}
只是在测试时对我的凭据进行硬编码。在签入之前,我们必须为此创建一个域帐户。
我可以通过浏览器点击 ReportService2005.asmx 和 ReportExecution2005.asmx (这是我的 ReportServerUrl 变成的)没有问题。
当我调用 SetParameters 时,我得到一个 WebException。查看异常中响应中的标头:
{RSNotAuthenticated: True RSAuthenticationHeader: .ASPXFORMSAUTH Content-Length: 206 Cache-Control: private Content-Type: text/html; charset=utf-8 日期:2013 年 9 月 24 日星期二 16:15:08 GMT 位置:/ReportServer/logon.aspx?ReturnUrl=%2freportserver%2fReportExecution2005.asmx 服务器:Microsoft-HTTPAPI/2.0 X-AspNet-Version:2.0。 50727
}
这似乎是在告诉我我还没有登录。如果是这种情况,完全相同的凭据如何允许我通过浏览器查看 Web 服务?
顺便说一句,我确实在 ReportServerCredentials 实现中的每个方法中设置了断点,并看到了每个断点命中。不确定这告诉我们什么,但 NetworkCredentials 接口返回了我的网络凭据就好了。