2

我正在使用此代码从 SSRS 创建和下载 PDF 报告,但收到以下错误:

远程服务器返回错误:(401) Unauthorized。

我的代码是:

protected void Page_Load(object sender, EventArgs e)
{
    MemoryStream ms;
    // this name is what the user will see when they are prompted for download.
    string customFileName = "NewFileName.pdf";

    if (!this.IsPostBack)
    {
        try
        {
            string strRequest = "http://myServer.com/ReportServer?%2fmetadata_report%2fMetadataReport1&rs%3aCommand=Render&rs%3AFormat=PDF&grpId=3";
            WebRequest request = WebRequest.Create(strRequest);
            request.ContentType = "application/pdf";

            string userName = "username";
            string password = "password";
            string domain = "myServer.com";

            // Create and then pass in network credentials to acecss the reporting server
            System.Net.NetworkCredential credentials = new NetworkCredential(userName, password, domain);
            request.Credentials = credentials;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (response)
            {
                // Get the stream from the server
                using (Stream stream = response.GetResponseStream())
                {
                    // Use the ReadFully method from the link above:
                    byte[] data = ReadFully(stream, response.ContentLength);

                    // Return the memory stream.
                    ms = new MemoryStream(data);
                }
            }

            // Clear out the headers that may already exist, then set content type and add a header to name the file
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "inline; ");

            //// Write the memory stream containing the pdf file directly to the Response object that gets sent to the client
            ms.WriteTo(Response.OutputStream);
        }
    }
}

任何帮助将非常感激。

4

3 回答 3

1

你能试试这个吗?

// Create and then pass in network credentials to acecss the reporting server
CredentialCache cc = new CredentialCache();
cc.Add(new Uri("http://myServer.com/"), "Negotiate", new NetworkCredential("Username", "Password", "Domain"));
request.Credentials = cc;
于 2012-09-13T22:22:23.963 回答
0

因为这个,过去一周我掉了很多头发。在我的案例中,报告定义与我的 Sql 服务器和 IIS 位于同一台服务器中。我通过从 ssrs 服务器中提取报告来测试我的开发机器上的所有内容。在这一点上,一切都工作得很好。现在,只要我在生产服务器中部署我的应用程序,它就开始给我这个错误。我有一个单独的报告用户,它使用在开发机器上工作的必要权限进行排序。微软的安全功能可以防止双跳。如果你用谷歌搜索 LSA,我相信你会得到足够的。请记住 impersonate=true 不会有任何好处。

以下是我的解决方案,它就像一个魅力:

方法 2:禁用身份验证环回检查通过将 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 注册表子项中的 DisableLoopbackCheck 注册表项设置为 1,重新启用 Windows Server 2003 中存在的行为。要将 DisableLoopbackCheck 注册表项设置为 1,请按照在客户端计算机上执行以下步骤: 单击开始,单击运行,键入 regedit,然后单击确定。找到并单击以下注册表子项: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 右键单击​​ Lsa,指向新建,然后单击 DWORD 值。键入 DisableLoopbackCheck,然后按 Enter。右键单击 DisableLoopbackCheck,然后单击修改。在数值数据框中,键入 1,然后单击确定。退出注册表编辑器。重新启动计算机。注意您必须重新启动服务器才能使此更改生效。默认情况下,Windows Server 2003 SP1 中打开了环回检查功能,并且 DisableLoopbackCheck 注册表项设置为 0(零)。当您禁用身份验证环回检查并打开 Windows Server 2003 服务器以对 NTLM 进行中间人 (MITM) 攻击时,安全性会降低。

详细研究:阅读这里

希望它可以帮助某人。

谢谢

于 2018-01-29T09:34:02.193 回答