SSRS报告查看器+ ASP.NET凭据401例外

Jar*_*yer 7 asp.net reporting-services http-status-code-401

我有一个保存在SQL2005报告服务器上的报告,我想返回此报告的呈现PDF.我在使用本地*.rdlc文件时已经想到了这一点(我已经在博客上发表过这篇文章),但是当*.rdl驻留在报表服务器上时却没有.我在线上收到401 Not Notized错误...

reportViewer.ServerReport.SetParameters(reportDefinition.ReportParameters);
Run Code Online (Sandbox Code Playgroud)

这是用于呈现报告的方法.

public byte[] Render(IReportDefinition reportDefinition)
{
    var reportViewer = new ReportViewer();
    byte[] renderedReport;
    try
    {
        var credentials = new WindowsImpersonationCredentials();
        reportViewer.ServerReport.ReportServerUrl = new Uri("http://myssrsbox", UrlKind.Absolute);
        reportViewer.ServerReport.ReportServerCredentials = credentials;
        reportViewer.ServerReport.ReportPath = reportDefinition.Path;
        // Exception is thrown on the following line...
        reportViewer.ServerReport.SetParameters(reportDefinition.ReportParameters);

        string mimeType;
        string encoding;
        string filenameExtension;
        string[] streams;
        Warning[] warnings;

        renderedReport = reportViewer.ServerReport.Render(reportDefinition.OutputType, reportDefinition.DeviceInfo, out mimeType, out encoding, out filenameExtension, out streams, out warnings);
    }
    catch (Exception ex)
    {
        // log the error...
        throw;
    }
    finally
    {
        reportViewer.Dispose();
    }
    return renderedReport;
}
Run Code Online (Sandbox Code Playgroud)

您缺少的另一件事是WindowsImpersonationCredentials类.

public class WindowsImpersonationCredentials : IReportServerCredentials
{
    public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
    {
        authCookie = null;
        userName = password = authority = null;
        return false;
    }

    public WindowsIdentity ImpersonationUser
    {
        get { return WindowsIdentity.GetCurrent(); }
    }

    public ICredentials NetworkCredentials
    {
        get { return null; }
    }

    public override string ToString()
    {
        return String.Format("WindowsIdentity: {0} ({1})", this.ImpersonationUser.Name, this.ImpersonationUser.User.Value);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能需要了解的其他事项......

  • 这是在Intranet上运行,并且模拟已打开.
  • 日志记录表示正在正确设置模拟用户.
  • 确实工作在Visual Studio(运行时http://localhost:devport),它不工作对我的开发机器上运行时,( http://localhost/myApplication).在我们的测试或生产服务器上运行时,它不起作用.
  • 我在web.config中尝试过使用和不使用system.net.defaultProxy设置的解决方案.都没有奏效.

我究竟做错了什么?它是服务器设置吗?是代码吗?是web.config吗?

Jar*_*yer 6

我们终于弄明白了这个问题.我们的网络管理员禁用了双跳,因此当模拟正确连接时domain\jmeyer,应用程序仍在尝试连接到SRS盒domain\web01$.为什么这样设置?因为双跳是一个巨大的安全漏洞.(或者有人告诉我.这听起来像是你会在每日WTF上看到吗?)

我们的解决方案是创建通用domain\ssrs_report_services用户,并使用以下网络凭据与该用户建立连接

public class CustomCredentials : IReportServerCredentials
{
    public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
    {
        authCookie = null;
        userName = password = authority = null;
        return false;
    }

    public WindowsIdentity ImpersonationUser
    {
        get { return null; }
    }

    public ICredentials NetworkCredentials
    {
        get { return new NetworkCredential("ssrs_report_services", "password", "domain") ; }
    }    
}
Run Code Online (Sandbox Code Playgroud)

以上是您可以在整个互联网上找到的经典示例解决方案.