SSRS ListRenderingExtension Unauthorised Exception

414 views Asked by At

I am currently using Report Viewer 11 to connect to a SQL Server 2008 r2 SSRS endpoint in order to run reports within a web page. Previously this was all working when SSRS and the DB were running on the same virtual server.

We just moved the DB and SSRS off the web server onto a new virtual instance and I am getting an 401 - Unauthorised Exception back when running the ServerReport.ListRenderingExtensions() method but calling the report parameters list using ServerReport.GetParameters() works without issue.

Below is the class that I am using to load the reports and I am populating the CustomReportCredentials with the admin username and password and the Domain is populated with the name of the new DB/SSRS server.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Web.UI;
using Microsoft.Reporting.WebForms;
using System.Reflection;

public partial class ReportViewer : Page 
{
    private string ReportingServer = ConfigurationManager.AppSettings["ReportViewerEndpoint"];

    private string UserName = ConfigurationManager.AppSettings["ReportViewerUser"];
    private string Password = ConfigurationManager.AppSettings["ReportViewerPassword"];
    private string Domain = ConfigurationManager.AppSettings["ReportViewerDomain"];

    protected void Page_Load(object sender, EventArgs e)
    {
        ssrsReportViewer.ServerReport.ReportServerUrl = new Uri(ReportingServer);

        if (!IsPostBack)
        {
            DisableUnwantedExportFormat();

            IReportServerCredentials irsc = new CustomReportCredentials(UserName, Password, Domain);
            ssrsReportViewer.ServerReport.ReportServerCredentials = irsc;
            SetReportPath();
            SetParameters();
        }
    }

    private void SetReportPath()
    {
        if (Request.QueryString["Path"] != null)
            ssrsReportViewer.ServerReport.ReportPath = Request.QueryString["Path"];
    }

    private void SetParameters()
    {
        if (!string.IsNullOrWhiteSpace(ssrsReportViewer.ServerReport.ReportPath))
        {
            List<string> filters = new List<string>();
            List<ReportParameterInfo> reportParameters = ssrsReportViewer.ServerReport.GetParameters().ToList();

            foreach (ReportParameterInfo param in reportParameters.Where(w => w.Nullable.Equals(true)))
                ssrsReportViewer.ServerReport.SetParameters(new ReportParameter(param.Name, new string[] { null }, false));

            foreach (string key in Request.QueryString)
            {
                string values = Request.QueryString[key];

                if (reportParameters.Any(r => r.Name.Equals(key)))
                {
                    ssrsReportViewer.ServerReport.SetParameters(new ReportParameter(key, values));
                    filters.Add(string.Format("{0} - {1}", key.ToUpper(CultureInfo.InvariantCulture), values));
                }
            }

            if (reportParameters.Any(r => r.Name.Equals("Filters")))
                ssrsReportViewer.ServerReport.SetParameters(new ReportParameter("Filters", string.Join("; ", filters)));
        }
    }

    private void DisableUnwantedExportFormat()
    {
        FieldInfo info;

        string[] removeFormats;
        string exclusionsUrl = Request.QueryString["ExcludedExports"];

        if (!string.IsNullOrWhiteSpace(exclusionsUrl))
        {
            removeFormats = exclusionsUrl.Split(',');

            foreach (RenderingExtension extension in ssrsReportViewer.ServerReport.ListRenderingExtensions())
             {
                foreach(string format in removeFormats )
                {
                    if (extension.Name.ToUpper().Equals(format.ToUpper()))
                    {
                        info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
                        info.SetValue(extension, false);
                    }
                }
            }
        }

    }
}

public class CustomReportCredentials : IReportServerCredentials
{
    private readonly string userName;
    private readonly string passWord;
    private readonly string domainName;

     public CustomReportCredentials(string userName, string passWord, string domainName)
     {
        this.userName = userName;
        this.passWord = passWord;
        this.domainName = domainName;
     }

     public WindowsIdentity ImpersonationUser
     { 
        get { return null; } 
     }  

     public ICredentials NetworkCredentials
     {
        get { return new NetworkCredential(userName, passWord, domainName); }
     }

     public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
     {
        authCookie = null;
        user = password = authority = null;
        return false;
     }
}

Any idea why I am getting this 401 - Unauthorised exception back from the ServerReport.ListRenderingExtensions() method?

1

There are 1 answers

1
Mike On BEST ANSWER

You are setting the credentials after calling:

DisableUnwantedExportFormat();

Change the code so that the credentials are set first:

        IReportServerCredentials irsc = new CustomReportCredentials(UserName, Password, Domain);
        ssrsReportViewer.ServerReport.ReportServerCredentials = irsc;

        DisableUnwantedExportFormat();
        SetReportPath();
        SetParameters();