【问题标题】:Using HttpClient inside ASP.NET MVC Action to call SSRS在 ASP.NET MVC Action 中使用 HttpClient 调用 SSRS
【发布时间】:2013-12-12 13:25:22
【问题描述】:

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

动作是

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

【问题讨论】:

  • 用户是否有权访问报告服务?
  • @scheien 是的,当我将 URL 粘贴到浏览器地址栏中或将链接用作 href 时,它可以工作。
  • 不应该有一个域名作为你的 NetworkCredential 构造函数的第三个参数吗?
  • @jbl 它有几个重载,所以我认为这不是问题。
  • 我知道 ;-) 关键是也许你应该在这个参数中放一些东西(比如域名,或者机器名)

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多