我目前正在使用Report Viewer 11连接到SQL Server 2008 r2 SSRS端点,以便在网页中运行报表。以前,当SSRS和数据库在同一个虚拟服务器上运行时,这一切都正常。
我们刚刚将数据库和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 - 未经授权的异常?
答案 0 :(得分:0)
您在调用后设置凭据:
DisableUnwantedExportFormat();
更改代码,以便首先设置凭据:
IReportServerCredentials irsc = new CustomReportCredentials(UserName, Password, Domain);
ssrsReportViewer.ServerReport.ReportServerCredentials = irsc;
DisableUnwantedExportFormat();
SetReportPath();
SetParameters();