【问题标题】:Unauthorized when passing credentials to report server from reportviewer从 reportviewer 向报表服务器传递凭据时未经授权
【发布时间】:2020-01-14 13:35:47
【问题描述】:

我有以下控制器(ReportsController.cs)

using Microsoft.Reporting.WebForms;
using System;
using System.Configuration;
using System.Web.Mvc;
using System.Net;

namespace ActiveDirectoryAuthentication.Controllers
{
    public class ReportsController : Controller
    {
        // GET: Reports
        public ActionResult Index()
        {
            string ssrsUrl = ConfigurationManager.AppSettings["SSRSReportsUrl"].ToString();
            ReportViewer viewer = new ReportViewer();
            IReportServerCredentials irsc = new CustomReportCredentials("uname", "pwd", "domain");
            viewer.ServerReport.ReportServerCredentials = irsc;
            viewer.ProcessingMode = ProcessingMode.Remote;
            viewer.SizeToReportContent = true;
            viewer.AsyncRendering = true;
            viewer.ServerReport.ReportServerUrl = new Uri(ssrsUrl);
            viewer.ServerReport.ReportPath = "/Temperature_Graphs_Reports/Temperature_Graphs";
            viewer.ServerReport.Refresh();

            ViewBag.ReportViewer = viewer;

            return View();
        }
    }

    public class CustomReportCredentials : IReportServerCredentials
    {
        private string _UserName;
        private string _PassWord;
        private string _DomainName;

        public CustomReportCredentials(string UserName, string PassWord, string DomainName)
        {
            _UserName = UserName;
            _PassWord = PassWord;
            _DomainName = DomainName;
        }

        public System.Security.Principal.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;
        }
    }
}

和以下视图(Index.cshtml):

@using ReportViewerForMvc;

@if (ViewBag.ReportViewer != null)
{
    @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)
}

我可以在不传递凭据(手动传递)的情况下查看报告。

通过凭据我收到以下错误消息:

“/”应用程序中的服务器错误。

请求失败,HTTP 状态为 401:未经授权。描述: 当前执行期间发生未处理的异常 网络请求。请查看堆栈跟踪以获取有关的更多信息 错误及其起源于代码的位置。

异常详细信息:System.Net.WebException:请求失败 HTTP 状态 401:未经授权。

来源错误:

在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

堆栈跟踪:

[WebException:请求失败,HTTP 状态 401:未授权。] Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.GetSecureMethods() +192 Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.IsSecureMethod(字符串 方法名)+51
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.SetConnectionSSLForMethod(字符串 方法名)+12
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ProxyMethodInvocation.Execute(RSExecutionConnection 连接,ProxyMethod1 initialMethod, ProxyMethod1 retryMethod) +449 Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.LoadReport(字符串 报告,字符串 HistoryID) +180
Microsoft.Reporting.WebForms.SoapReportExecutionService.LoadReport(字符串 报告,字符串 historyId) +24
Microsoft.Reporting.WebForms.ServerReport.EnsureExecutionSession() +70 Microsoft.Reporting.WebForms.ServerReport.GetParameters() +54
ReportViewerForMvc.ReportViewerExtensions.SetProperties(ServerReport serverReport、ServerReport 属性)+60
ReportViewerForMvc.ReportViewerExtensions.SetProperties(ReportViewer reportViewer、ReportViewer 属性)+154
ReportViewerForMvc.ReportViewerWebForm.BuildReportViewer() +67
ReportViewerForMvc.ReportViewerWebForm.Page_Load(对象发送者, 事件参数 e) +5
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者, EventArgs e) +52 System.Web.UI.Control.OnLoad(EventArgs e) +97
System.Web.UI.Control.LoadRecursive() +61
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +693

这是rsreportserver.config的值

<AuthenticationTypes>            
  <RSWindowsNTLM/>
</AuthenticationTypes>

Web Portal URL 下的Report Server Configuration Manager 中,当我单击URL 时出现此错误消息

The service is not available. The report server isn’t configured properly. Contact your system administrator to resolve the issue. System administrators: The report server Web Portal URLs and Web Service URLs don’t match. Use Reporting Services Configuration Manager to configure the URLs and ensure they match.

【问题讨论】:

  • This thread 可能会帮助您解决配置问题
  • @timur 谢谢。我修复了配置。在报告管理器中,我设置了通过数据源上的数据库帐户登录,这很有帮助。您的回答和评论也非常有用。

标签: c# asp.net reporting-services ssrs-2012 reportviewer


【解决方案1】:

据我所知,您的代码很好。我刚刚快速创建了一个 boilerplate MVC project 来针对我的 SSRS 实例进行测试,并成功运行它而无需更改您的代码。

所以看来问题在于您正在运行的环境:

  1. Check if you SSRS is configured 接受您的特定身份验证请求(默认情况下它应该与 NTLM 和 Negotiate 一起提供,但您没有指定是否仍然是这种情况)。

  2. ReportViewer 控件本身依赖于 ASP.NET 会话来保持状态,所以 check if you need to work around 就是这样。

由于我对您的具体设置了解不多,因此无法更具体,但希望这可以为您提供一些探索的选择。

【讨论】:

  • 我更新了问题。我不知道your链接中Realm下应该是什么值
猜你喜欢
  • 1970-01-01
  • 2015-04-14
  • 2018-08-20
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多