Friday, July 18, 2008

How to pass windows authentication when serving a report with Report Viewer from Reporting Services

First create the following class:

[Serializable]
public sealed class ReportServerNetworkCredentials : IReportServerCredentials
{
#region IReportServerCredentials Members

///
/// Provides forms authentication to be used to connect to the report server.
///

/// A Report Server authentication cookie.
/// The name of the user.
/// The password of the user.
/// The authority to use when authenticating the user, such as a Microsoft Windows domain.
///
public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName,
out string password, out string authority)
{
authCookie = null;
userName = null;
password = null;
authority = null;

return false;
}

///
/// Specifies the user to impersonate when connecting to a report server.
///

///
/// A WindowsIdentity object representing the user to impersonate.
public WindowsIdentity ImpersonationUser
{
get
{
return null;
}
}

///
/// Returns network credentials to be used for authentication with the report server.
///

///
/// A NetworkCredentials object.
public System.Net.ICredentials NetworkCredentials
{
get
{
string userName = "username";
string domainName = "domain or computer";
string password = "password";

return new System.Net.NetworkCredential(userName, password, domainName);
}
}

#endregion
}

Then in the code behind of the page with the report viewer, do the following on page load:

protected void Page_Load(object sender, EventArgs e)
{
myReportViewer.ServerReport.ReportServerCredentials = new ReportServerNetworkCredentials();

}

Some notes about this. When working locally, I could get the report view to display without prompting me for a password (the authentication worked), but the data wouldn'd display. I'm not sure if this is a Vista issue or what, but when I publised it to the server, everything worked fine. Well, everything worked fine after I download the Microsoft Report Viewer Redistributable 2008 from mirosoft and installed it. You can download it here: http://www.microsoft.com/downloads/details.aspx?FamilyID=CC96C246-61E5-4D9E-BB5F-416D75A1B9EF&displaylang=en. This is necessary because the .net Report View control is part of VS 2008, so not included on the server.

0 comments: