Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HttpClient inside ASP.NET MVC Action to call SSRS

I can't seem to send a request to report server using HttpClient as the result is always 401 (Unauthorized).

The action is

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

Additional:

Pass a Report Parameter Within a URL

Export a Report Using URL Access

Export Formats

Generate Data Feeds from a Report

like image 531
Matija Grcic Avatar asked Dec 12 '13 13:12

Matija Grcic


1 Answers

I've used Fiddler to see what is going on when i login using browser

enter image description here

the Auth tab is

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

so even though I was told that the authentication is Basic i needed to use the following

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

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

The rest of the code for HttpClient is the same.

Additional:

Authentication with the Report Server

Selecting a Credential Type

Understanding SQL Server Reporting Services Authentication

Configure Basic Authentication on the Report Server

like image 60
Matija Grcic Avatar answered Sep 18 '22 11:09

Matija Grcic