Cos*_*lis 4 c# sql-server asp.net windows-security reporting-services
(使用C#,Web API,带有报表服务器的SQL Server2012,身份验证为NTLM)
尝试从SSRS下载报告(作为Excel文档)时出现间歇性错误。我建立正确的URL来呈现报告,如下所示:
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("myDom\\myReportReader", "P@55W0rd");
//string credentials = Convert.ToBase64String(
// Encoding.ASCII.GetBytes("myDom\\myReportReader" + ":" + P@55W0rd"));
//webClient.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
//401 Unauthorized thrown here:
return new MemoryStream(webClient.DownloadData(reportUrl));
Run Code Online (Sandbox Code Playgroud)
这里的目标是面向公众的IIS上的Web API控制器从受内部/防火墙保护的SSRS下载文件流,然后将流中继到浏览器。有时候,这行之有效...当它不行时,它在最后一行返回401错误...上面标注的行表示试图解决不起作用的问题。
小智 5
一种解决方案是更改请求的URL。
当您想通过ReportViewer.aspx导出报告时,SSRS通过重定向(http状态302)进行响应。并且WebClient不会将凭据重新发送到重定向的页面。
您应该以以下格式请求报告: http:// sqlServer / ReportServer?/ TheReportName&rs:Format = EXCEL&rc:Parameters ...因此只需从URL删除此序列:/Pages/ReportViewer.aspx即可。
有关使用URL访问导出报告的更多信息,请访问:https : //msdn.microsoft.com/zh-cn/library/ms154040.aspx
另一种解决方案是保留非最佳URL并使用CredentialCache,它将为重定向的页面提供凭据,但不要忘记Uri仅将参数设置为URL 前缀,即“ http:// sqlServer”或“ http:”。 // sqlServer / ReportServer”,而不是更多。
WebClient webClient = new WebClient();
var nc = new NetworkCredential("myReportReader", "P@55W0rd", "myDom");
var cc = new CredentialCache{{new Uri("http://sqlServer"), "Ntlm", nc}};
webClient.Credentials = cc;
return new MemoryStream(webClient.DownloadData(reportUrl));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1315 次 |
| 最近记录: |