在 ASP.NET MVC Action 中使用 HttpClient 调用 SSRS

Mat*_*cic 7 c# asp.net asp.net-mvc reporting-services

我似乎无法使用HttpClient向报告服务器发送请求,因为结果总是 401 (Unauthorized)

动作是

public async Task<FileStreamResult> SSRSTest()
            {

                //set credentials
                using (var handler = new HttpClientHandler { 
                    Credentials = new NetworkCredential("userName", "password"),
                    UseDefaultCredentials = false
                })

                using (var httpClient = new HttpClient(handler))
                {
                    //get *.pdf from report server
                    var response = await httpClient                      .GetStreamAsync("http://someip/ReportServer/Pages/ReportViewer.aspx?TheRemainingPartOfURL");


                    var contentDisposition = new ContentDisposition
                    {
                        FileName = "SomeReport.pdf",
                        Inline = false
                    };

                    //set content disposition
                    Response.AppendHeader("Content-Disposition", contentDisposition.ToString());

                    //return the file
                    return File(response, "application/pdf");
                }
            }
Run Code Online (Sandbox Code Playgroud)

额外的:

在 URL 内传递报告参数

使用 URL 访问导出报告

导出格式

从报告生成数据馈送

Mat*_*cic 5

我使用 Fiddler 来查看当我使用浏览器登录时发生了什么

在此输入图像描述

身份验证选项卡是

WWW-Authenticate Header is present: Negotiate    
WWW-Authenticate Header is present: NTLM
Run Code Online (Sandbox Code Playgroud)

所以即使我被告知身份验证是基本的,我也需要使用以下内容

 CredentialCache credentialCache = new CredentialCache();
            credentialCache.Add(new Uri("http://youruri"),"NTLM",new NetworkCredential(username, password));

            using (var handler = new HttpClientHandler
            {
                Credentials = credentialCache
            })
Run Code Online (Sandbox Code Playgroud)

HttpClient的其余代码是相同的。

额外的:

使用报表服务器进行身份验证

选择凭证类型

了解 SQL Server Reporting Services 身份验证

在报表服务器上配置基本身份验证