Problems to authenticate on the 2014 Report Service server via C # application MVC5

4

I'm having a problem connecting to the 2014 Report Service via C # from an MVC4 project:

Scenery:

  • A 2014 Report Service Server was installed that will have numerous reports, due to the client having numerous SQL Server databases spread across the state of SP. the most viable Solution was this because in the Report Server we have how to add several tals connections (DataSources).

So far, okay.

Problem:

  • There is an MVC5 application that will have a list of these reports, where the Client (user) will open tals to see. The question is "Can not connect to the Report Service Server", the error is:

    The remote server returned an error: (401) Não Autorizado.

    Ex: Code

    public FileContentResult teste()
    {
        NetworkCredential Credential = new NetworkCredential("/USER/", "/PASS/");
        WebClient client = new WebClient();
        client.Credentials = Credential;
    
        string reportURL = "http://localhost/report/Pages/ReportViewer.aspx?/Relatorios/ColaboradoresRpt&rs:Command=Render&rs:Format=pdf";
        return File(client.DownloadData(reportURL), "application/pdf");
    }
    
asked by anonymous 06.05.2015 / 14:53

1 answer

1

It appears that WebClient is not passing credentials to the Report Service.

In its place, I would try to access it using HttpClient with HttpClientHandler to further specify the authentication data.

In the example below, sending the authentication header serves the REST pattern well. As I am not familiar with the Report Service authentication protocol, I would say the default is similar, but some adjustment may be necessary:

using (var client = new HttpClient()) {
    var byteArray = Encoding.ASCII.GetBytes("Usuario:Senha");
    var header = new AuthenticationHeaderValue(
               "Basic", Convert.ToBase64String(byteArray));
    client.DefaultRequestHeaders.Authorization = header;

    var result = await client.GetByteArrayAsync(uri);
}
    
06.05.2015 / 16:52