将凭据从报表查看器传递到报表服务器时未经授权
Unauthorized when passing credentials to report server from reportviewer
我有以下控制器 (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)
}
我可以在不传递凭据(手动传递)的情况下查看报告。
通过凭据我收到此错误消息:
Server Error in '/' Application.
The request failed with HTTP status 401: Unauthorized. Description:
An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Net.WebException: The request failed with
HTTP status 401: Unauthorized.
Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Stack Trace:
[WebException: The request failed with HTTP status 401: Unauthorized.]
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.GetSecureMethods()
+192 Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.IsSecureMethod(String
methodname) +51
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.SetConnectionSSLForMethod(String
methodname) +12
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ProxyMethodInvocation.Execute(RSExecutionConnection
connection, ProxyMethod1 initialMethod, ProxyMethod
1 retryMethod)
+449 Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.LoadReport(String
Report, String HistoryID) +180
Microsoft.Reporting.WebForms.SoapReportExecutionService.LoadReport(String
report, String historyId) +24
Microsoft.Reporting.WebForms.ServerReport.EnsureExecutionSession() +70
Microsoft.Reporting.WebForms.ServerReport.GetParameters() +54
ReportViewerForMvc.ReportViewerExtensions.SetProperties(ServerReport
serverReport, ServerReport properties) +60
ReportViewerForMvc.ReportViewerExtensions.SetProperties(ReportViewer
reportViewer, ReportViewer properties) +154
ReportViewerForMvc.ReportViewerWebForm.BuildReportViewer() +67
ReportViewerForMvc.ReportViewerWebForm.Page_Load(Object sender,
EventArgs e) +5
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) +52 System.Web.UI.Control.OnLoad(EventArgs e) +97
System.Web.UI.Control.LoadRecursive() +61
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+693
这是rsreportserver.config
的值
<AuthenticationTypes>
<RSWindowsNTLM/>
</AuthenticationTypes>
在 Report Server Configuration Manager
下的 Web Portal URL
中单击 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.
据我所知,您的代码没有问题。我只是快速 boilerplate MVC project 来针对我的 SSRS 实例对其进行测试并成功 运行 它没有更改您的代码。
看来问题出在你所在的环境 运行:
Check if you SSRS is configured 接受您的特定身份验证请求(默认情况下它应该与 NTLM 和协商一起提供,但您没有指定是否仍然是您的情况)。
ReportViewer控件本身依赖ASP.NET会话来保持状态,所以check if you need to work around那个。
由于我不太了解您的具体设置,因此无法更具体地说明,但希望这能为您提供一些可供探索的选择。
我有以下控制器 (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)
}
我可以在不传递凭据(手动传递)的情况下查看报告。
通过凭据我收到此错误消息:
Server Error in '/' Application.
The request failed with HTTP status 401: Unauthorized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The request failed with HTTP status 401: Unauthorized.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[WebException: The request failed with HTTP status 401: Unauthorized.] Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.GetSecureMethods() +192 Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.IsSecureMethod(String methodname) +51
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.SetConnectionSSLForMethod(String methodname) +12
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ProxyMethodInvocation.Execute(RSExecutionConnection connection, ProxyMethod1 initialMethod, ProxyMethod
1 retryMethod) +449 Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.LoadReport(String Report, String HistoryID) +180
Microsoft.Reporting.WebForms.SoapReportExecutionService.LoadReport(String report, String historyId) +24
Microsoft.Reporting.WebForms.ServerReport.EnsureExecutionSession() +70 Microsoft.Reporting.WebForms.ServerReport.GetParameters() +54
ReportViewerForMvc.ReportViewerExtensions.SetProperties(ServerReport serverReport, ServerReport properties) +60
ReportViewerForMvc.ReportViewerExtensions.SetProperties(ReportViewer reportViewer, ReportViewer properties) +154
ReportViewerForMvc.ReportViewerWebForm.BuildReportViewer() +67
ReportViewerForMvc.ReportViewerWebForm.Page_Load(Object sender, EventArgs e) +5
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +52 System.Web.UI.Control.OnLoad(EventArgs e) +97
System.Web.UI.Control.LoadRecursive() +61
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +693
这是rsreportserver.config
<AuthenticationTypes>
<RSWindowsNTLM/>
</AuthenticationTypes>
在 Report Server Configuration Manager
下的 Web Portal URL
中单击 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.
据我所知,您的代码没有问题。我只是快速 boilerplate MVC project 来针对我的 SSRS 实例对其进行测试并成功 运行 它没有更改您的代码。
看来问题出在你所在的环境 运行:
Check if you SSRS is configured 接受您的特定身份验证请求(默认情况下它应该与 NTLM 和协商一起提供,但您没有指定是否仍然是您的情况)。
ReportViewer控件本身依赖ASP.NET会话来保持状态,所以check if you need to work around那个。
由于我不太了解您的具体设置,因此无法更具体地说明,但希望这能为您提供一些可供探索的选择。