An Idea can change your life.....

Wednesday, December 05, 2007

Visual Studio » Visual Studio Report Controls » ReportServerCredentials

The scenario is simple: I have a reportviewer control and want to show & print a ServerReport.

The application needs to be able to connect to the reporting service over the internet and the client machines do not need to be in the same domain.

This is the same authentication scenario that applies to viewing reports through HTTP using internet explorer and a URL to the reportserver virtual directory: Internet Explorer asks for a user and password.

Howerver, I want to specify the credentials programatically so the users don't need to enter them manually. Since the ReportViewer is connecting to the ReportServer website through HTTP (I guess its calling the webservice or even just displaying the HTML returned by the reportserver aspx), this should be pretty easy, just as its easy to setup a NetworkCredential(user, pass) for authentication when calling a webservice over http.

The solution follows





well , you should implement IReportServerCredential, and code example as follow :



public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
{


// local variable for network credential.
private string _UserName;
private string _PassWord;
private string _DomainName;
public CustomReportCredentials(string UserName, string PassWord, string DomainName)
{
_UserName = UserName;
_PassWord = PassWord;
_DomainName = DomainName;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null; // not use ImpersonationUser
}
}
public ICredentials NetworkCredentials
{
get
{



// use NetworkCredentials
return new NetworkCredential(_UserName,_PassWord,_DomainName);
}
}
public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
{


// not use FormsCredentials unless you have implements a custom autentication.
authCookie = null;
user = password = authority = null;
return false;
}


}


then use this as follows:



IReportServerCredentials irsc = new CustomReportCredentials(userid,password, domain);
ReportViewer1.ServerReport.ReportServerCredentials = irsc;

The other code to set ServerReport property omit. hope you have a good day.




1 comment:

Unknown said...

Is it a must to implement IReportServerCredentials ?