【问题标题】:SSRS ListRenderingExtension Unauthorised ExceptionSSRS ListRenderingExtension 未经授权的异常
【发布时间】:2015-06-11 21:45:49
【问题描述】:

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

我们刚刚将 DB 和 SSRS 从 Web 服务器移到一个新的虚拟实例上,运行 ServerReport.ListRenderingExtensions() 方法时我得到了 401 - Unauthorized Exception,但使用 ServerReport.GetParameters() 调用报告参数列表没有问题。

下面是我用来加载报告的类,我正在使用管理员用户名和密码填充 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 - Unauthorized 异常吗?

【问题讨论】:

    标签: sql-server reporting-services reportviewer


    【解决方案1】:

    您正在调用后设置凭据:

    DisableUnwantedExportFormat();
    

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

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

    【讨论】:

    • 很好。不敢相信我错过了。
    猜你喜欢
    • 2019-03-11
    • 2012-02-22
    • 2014-07-01
    • 1970-01-01
    • 2015-04-14
    • 2018-08-21
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多