Я использовал этот код в моем проекте, и он работает.Сначала вам нужно создать пользовательский класс учетных данных, который реализует интерфейс IReportServerCredentials.
public class ReportServerCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
{
#region private members
private string _username;
private string _password;
private string _domain;
#endregion
#region Constructor
/// <summary>
/// Initializes itself with ReportServerUsername, ReportServerPassword and ReportServerDomain settings from web.config
/// </summary>
public ReportServerCredentials()
{
this._username = "USERNAME";
this._password = "PASSWORD";
this._domain = ""; // set if its domain server
}
public ReportServerCredentials(string username, string password, string domain)
{
this._username = username;
this._password = password;
this._domain = domain;
}
#endregion
#region IReportServerCredentials Members
public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = password = authority = null;
return false;
}
public System.Security.Principal.WindowsIdentity ImpersonationUser
{
get { return null; }
}
/// <summary>
/// Creates a System.Net.NetworkCredential object with the specified username, password and domain.
/// </summary>
public System.Net.ICredentials NetworkCredentials
{
get { return new System.Net.NetworkCredential(_username, _password, _domain); }
}
#endregion
}
Передайте учетные данные в отчет перед его отображением.
ReportParameter[] param = new ReportParameter[0];
protected void Page_Load(object sender, EventArgs e)
{
this.ReportViewer1.Reset();
this.ReportViewer1.ServerReport.ReportServerUrl =
new System.Uri(ConfigurationManager.AppSettings["ReportServerUrl"]); // reads report server url from config file
IReportServerCredentials customCred =
new ReportServerCredentials(); //reads the username, password and domain from web.config
this.ReportViewer1.ServerReport.ReportServerCredentials = customCred;
this.ReportViewer1.ServerReport.ReportPath =
"/PATH_TO_SOME_REPORT"; // TODO: Put some real report path
this.ReportViewer1.ServerReport.SetParameters(param); // this will initialize report
}