ReportViewer control in web application story
Most articles out there talk about adding a report viewer control in Visual Studio to a windows form. If we need to add the same control to a web application, it isn't as straight forward. The following is my ReportViewerControl in web app story.
After hours of trial and error and frustration, this is what worked for me.
- Follow the directions at http://msdn.microsoft.com/en-us/library/ms251669.aspx to create your web application.
- My report was running on native mode so the report server url was http://(myserver-name)/ReportServer and the report path does NOT have a .rdl extension.
- Next we have to implement the interface IReportServerCredentials to authenticate through to the server that hosts the actual report. It was not easy to find one clean solution and therefore I blog about it.
using System;
using System.Configuration;
using System.Net;
using System.Security.Principal;
using Microsoft.Reporting.WebForms;
public partial class POCReportViewerControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
ReportViewer1.ServerReport.ReportServerCredentials =
new ReportServerCredentials();
}
}
public class ReportServerCredentials : IReportServerCredentials
{
private string _userName;
private string _password;
private string _domain;
public ReportServerCredentials()
{
_userName = ConfigurationManager.AppSettings.Get("username");
_password = ConfigurationManager.AppSettings.Get("password");
_domain = ConfigurationManager.AppSettings.Get("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 = password = authority = null;
return false;
}
}
- Set the identity configuration to impersonate ="true" in the web.config
- Once this worked fine then I moved on to storing the user's cookie info to reduce the authentication calls. A good article for this is found here http://praveenbattula.blogspot.com/2010/01/report-viewer-control-authentication_16.html
Comments
Post a Comment