Nuttiness of SQL Server Reporting Services ASP.NET Report Viewer Control

Recently I was attempting to create a simple test ASP.NET web application for viewing reports. The test was to use our administrative reports just to see how the Report Viewer control worked. Our SQL Reports Server was configured to use NT Integrated authentication, despite it being hosted on a server outside our network. I added the control to my web form like so:

<rsweb:ReportViewer ID="ReportViewer1" runat="server">
    <ServerReport ReportServerUrl="http://www.ourserver.com/ourreportserver" ReportPath="/Test/Test Report" />
</rsweb:ReportViewer>

And thought to myself "now I’ll just hard-code the credentials into the control in code" but to my surprise there was no straight forward way to do this… Searches on the internet revealed I needed to create a class that implemented IReportServerCredentials. Fair enough, kind of surprising MS didn’t do this for me when all I wanted was to pass a NetworkCredential. Anyway, I add the following class to my project:

public class ReportCredentials : IReportServerCredentials
{
    #region IReportServerCredentials Members
    
    public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
        {
            //throw new NotImplementedException(); 
            userName = password = authority = null;
            authCookie = null;
        return true;
        }

        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get { return null; }
        }

        public System.Net.ICredentials NetworkCredentials
        {
            get {
                return new NetworkCredential(@"reportserveruser", "reportserverpassword");
            }
        }

        #endregion
}

Unfortunately this does not work and I receive the error Logon Failed (rsLogonFailed), upon debugging I noticed that GetFormsCredentials was being called, and this confused me, despite the fact the Report Server only uses Integrated Windows auth. But to placate the server I made the following alteration to my class to make it return both a NetworkCredential and create a FormsCredential:

public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
{
    //throw new NotImplementedException(); 
    userName = password = authority = null;
    userName = "reportserveruser";
    password = "reportserverpassword";
    // The cookie name is specified in the <forms> element in Web.config (.ASPXAUTH by default)
    HttpCookie cookie = HttpContext.Current.Request.Cookies[".ASPXAUTH"];
    if (cookie == null) return false;
        //HttpContext.Current.Response.Redirect("login.aspx");
    Cookie netCookie = new Cookie(cookie.Name, cookie.Value);
    if (cookie.Domain == null)
    {
        netCookie.Domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToUpper();
    }
    netCookie.Expires = cookie.Expires;
    netCookie.Path = cookie.Path;
    netCookie.Secure = cookie.Secure;
    authCookie = netCookie;
    return true;
}

Success! the report works beautifully. So here’s the tip, when using NT Authentication you need to return both a NetworkCredential() from NetworkCredentials property and fill the output parameters of GetFormsCredentials with a valid AuthCookie (I assume this requirement has something to do with ASP.NET more than Reporting Services itself).

Advertisements
  1. #1 by John on August 16, 2008 - 11:57 pm

    I really appreciate this information that you published beacuse it got my reportviewer finally working after a week of trials with the reporting services that showed me a week of hell really and almost gave up on this task for the first time of my life but finally I found this info. Thanks a lot again

  2. #2 by Andrew on September 14, 2008 - 11:42 pm

    Glad my painful experience was able to help someone, thanks for commenting, it makes it all worthwhile.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: