From: http://space.itpub.net/9079672/viewspace-416169
The reportviewer control in webform cannot directly pass in identity creden。 because its serverreport. reportservercredentials. networkcredentials attribute is read-only.
After harassing N. Net MVPs, I finally realized that there was an API called ireportservercredentials. Why did I forget this. The custom authentication method in report service depends on it.
Since it is an interface, we must first use this interface to create a class, and then implement the members of this interface in the class. The ireportservercredentials interface consists of two member attributes (impersonateuser and networkcredentials) and one member method (getformcredentials ).
Impersonateuser is of little use here, haha, so return NULL directly. Networkcredentials is what we need to return, so return New networkcredential (_ username, _ password, _ domain ).
Where do _ username, _ password, and _ domain come from? Put three private variables in the class, and then pass them in through the class initialization method.
In this way, we can get it done. The following are the source code of the class and the source code of the calling class:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. net;
Using system. Web. sessionstate;
Using system. Security. Principal;
Using system. Collections. Generic;
Using Microsoft. Reporting. webforms;
Public partial class reportviewer: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Rptviewer. processingmode = processingmode. Remote;
Rptviewer. serverreport. reportserverurl = new uri ("http: // server name/reportserver ");
Rptviewer. serverreport. reportpath = "/testing/employeelist ";
Reportviewercredentials. myreportviewercredential RVC = new reportviewercredentials. myreportviewercredential ("user", "password ","");
Rptviewer. serverreport. reportservercredentials = RVC;
// New system. net. networkcredential ("reportuser", "userreport ");
Export cecredentials datasource = new export cecredentials ();
Datasource. Name = "Data Source Name ";
Datasource. userid = "userid ";
Datasource. Password = "password ";
Rptviewer. serverreport. setdatasourcecredentials (New datasourcecredentials [] {datasource });
List <reportparameter> paramlist = new list <reportparameter> ();
Paramlist. Add (New reportparameter ("emp_no", "", false ));
Paramlist. Add (New reportparameter ("birth_date_from", "1980-01-01", false ));
Paramlist. Add (New reportparameter ("birth_date_to", "1982-01-01", false ));
This. rptviewer. serverreport. setparameters (paramlist );
}
}
Public class reportviewercredentials
{
Public class myreportviewercredential: ireportservercredentials
{
Private string _ username;
Private string _ password;
Private string _ domain;
Public URI reportserverurl;
Public myreportviewercredential (string username, string password, string domain)
{
_ Username = username;
_ Password = password;
_ Domain = domain;
}
Public windowsidentity impersonationuser
{
Get {return NULL ;}
}
Public System. net. icredentials networkcredentials
{
Get
{
Return new networkcredential (_ username, _ password, _ domain );
}
}
Public bool getformscredentials (out Cookie authcookie,
Out string user,
Out string password,
Out string Authority)
{
Authcookie = NULL;
User = _ username;
Password = _ password;
Authority = _ domain;
Return false;
}
}
}