ReportViewer 错误 401 查看用户登录

ReportViewer Error 401 view user logging in

我在测试服务器上使用 ReportViewer 时收到 401 未授权错误。在我的本地环境中(配置指向测试服务器),我可以毫无问题地进行连接。我试过寻找以下解决方案但没有运气。 two three

我使用的身份验证模式是 Windows,虽然我的 windows 凭据可以访问报表服务器,但我仍然无法连接。

在我的web.config

      <system.web>
        <customErrors mode="Off" />
        <authentication mode="Windows" />
        <authorization>
          <deny users="?" />
        </authorization>                
         ...

我的控制器

private ReportViewer _reportViewer;

public ReportController()
{
    _reportViewer = new ReportViewer();
    _reportViewer.ProcessingMode = ProcessingMode.Remote;
    _reportViewer.SizeToReportContent = false;
    _reportViewer.Width = Unit.Percentage(100);
    _reportViewer.Height = Unit.Pixel(893);
    string RptServerURL = ConfigurationManager.AppSettings["MyReportServerUrl"];

    _reportViewer.ServerReport.ReportServerUrl = new Uri(RptServerURL);
}

这样我就可以查明错误,如何在哪里我可以看到我的代码正在尝试验证的用户吗?我检查了异常,但我得到的所有信息都是未经授权的,没有告诉我正在使用的用户。

我找到了答案,更重要的是如何解决导致 401 错误的问题。

首先回答我的问题,我可以通过检查

来找出用户
System.Security.Principal.WindowsIdentity.GetCurrent().Name

当 运行 我的本地环境中的应用程序(报表服务器指向测试 url)时,它正在使用 Windows 登录。在测试服务器下,它正在使用IIS APPPOOL。我尝试手动设置报告服务器凭据但没有成功。

ReportViewer rv;
rv.ServerReport.ReportServerCredentials = new ReportServerCredentials("userName", "password", "domain");

但是,我通过检查 IIS 管理器注意到登录的来源。

当我将身份更改为 NetworkService 时,应用程序能够正确访问报表服务器。