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

标签 c# asp.net asp.net-mvc reporting-services

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

Action 是

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");
                }
            }

附加:

Pass a Report Parameter Within a URL

Export a Report Using URL Access

Export Formats

Generate Data Feeds from a Report

最佳答案

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

enter image description here

Auth 标签是

WWW-Authenticate Header is present: Negotiate    
WWW-Authenticate Header is present: NTLM

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

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

            using (var handler = new HttpClientHandler
            {
                Credentials = credentialCache
            })

HttpClient 的其余代码是相同的。

附加:

Authentication with the Report Server

Selecting a Credential Type

Understanding SQL Server Reporting Services Authentication

Configure Basic Authentication on the Report Server

关于c# - 在 ASP.NET MVC Action 中使用 HttpClient 调用 SSRS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20544796/

相关文章:

c# - 如何使用 Facebook 开发者工具包将内容发布到留言墙上?

asp.net - Web API IIS 要求

asp.net-mvc - 在 Nopcommerce 中创建插件时出错

javascript - 如何将数据放入下拉框中并能够通过键入进行搜索?

javascript - 如何在 MVC 中为图表实现动态数据?

c# - 存储应用程序设置 |注册表

c# - 如何访问动态程序集中的匿名字段?

c# - "BeginSession"方法未被调用或未成功 - QBFC SDK12 和 ASP.NET 网站

javascript - 在 JavaScript 中输出 ASP.NET 模型

c# - 发送 ajax POST 请求接收 GET 请求