SSRS ListRenderingExtension 未经授权的异常

SSRS ListRenderingExtension Unauthorised Exception

我目前正在使用 Report Viewer 11 连接到 SQL Server 2008 r2 SSRS 端点,以便 运行 在网页中生成报告。以前,当 SSRS 和数据库 运行 在同一个虚拟服务器上时,这一切都有效。

我们刚刚将 DB 和 SSRS 从 Web 服务器移到一个新的虚拟实例上,当 运行 调用 ServerReport.ListRenderingExtensions() 方法但调用报告参数时,我收到了 401 - 未经授权的异常使用 ServerReport.GetParameters() 的列表可以正常工作。

下面是我用来加载报告的 class,我用管理员用户名和密码填充 CustomReportCredentials,域填充新 DB/SSRS服务器。

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;
     }
}

知道为什么我从 ServerReport.ListRenderingExtensions() 方法返回这个 401 - 未经授权的异常吗?

您正在调用后设置凭据:

DisableUnwantedExportFormat();

更改代码以便首先设置凭据:

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

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