Asp.net mvc 4 在MVC4中呈现远程ReportViewer控件时提示的凭据
我正在创建一个web应用程序(mvc 4),以授权客户(使用会员资格提供商)查看他们注册的报告(SSRS 2008),但他们无权访问我们的报告服务器 基于这个链接,我实现了Elsimer的最新答案,它可以作为pdf文件下载 但是,当我尝试使用上面链接中提到的相同代码呈现为html时,它要求windows凭据来访问报表服务器 因此,我提供了一个通用凭据,该凭据可以通过代码访问reportserver中的所有报告。但是,当报表服务器试图在客户端浏览器中以html形式查看时,它仍在请求报表服务器的凭据。正在渲染报表,但如果没有凭据,则不会渲染图像和图形 请告诉我,我已经尝试了很多方法来解决这个问题。但是没有运气 我的控制器和凭证类别代码如下:Asp.net mvc 4 在MVC4中呈现远程ReportViewer控件时提示的凭据,asp.net-mvc-4,reporting-services,reportviewer,credentials,Asp.net Mvc 4,Reporting Services,Reportviewer,Credentials,我正在创建一个web应用程序(mvc 4),以授权客户(使用会员资格提供商)查看他们注册的报告(SSRS 2008),但他们无权访问我们的报告服务器 基于这个链接,我实现了Elsimer的最新答案,它可以作为pdf文件下载 但是,当我尝试使用上面链接中提到的相同代码呈现为html时,它要求windows凭据来访问报表服务器 因此,我提供了一个通用凭据,该凭据可以通过代码访问reportserver中的所有报告。但是,当报表服务器试图在客户端浏览器中以html形式查看时,它仍在请求报表服务器的凭据
[Route("report/MonthlySummary")]
[ValidateAntiForgeryToken]
public ActionResult MonthlySummary(MonthlyReportParameters model)
{
if (ModelState.IsValid)
{
try
{
var actionType = model.ActionType;
if (actionType == "View Report")
{
return ExportMonthlyReportToHtml(model);
}
else if (actionType == "Download pdf report")
{
return ExportMonthlyReportToPdf(model);
}
}
catch (Exception ex)
{
//Logging errors
}
}
return null;
}
private ActionResult ExportMonthlyReportToHtml(MonthlyReportParameters monthlyParams)
{
ReportViewer reportViewer = BuildMonthlyReport(monthlyParams);
reportViewer.ServerReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streamids = null;
Warning[] warnings = null;
//To view the report in html format
streamBytes = reportViewer.ServerReport.Render("HTML4.0", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
var htmlReport = File(streamBytes, "text/html");
return htmlReport;
}
private static ReportViewer BuildMonthlyReport(MonthlyReportParameters model)
{
ReportViewer reportViewer = new Microsoft.Reporting.WebForms.ReportViewer();
try
{
var rptParameters = new List<ReportParameter>
{
//Building parameters
};
reportViewer.ProcessingMode = ProcessingMode.Remote;
reportViewer.ServerReport.ReportPath = "/reportFolder/reportName";
var reportServerUrl = ConfigurationManager.AppSettings["ReportServerUrl"];
if(!string.IsNullOrEmpty(reportServerUrl))
{
reportViewer.ServerReport.ReportServerUrl = new Uri(reportServerUrl);
}
reportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
reportViewer.ServerReport.SetParameters(rptParameters);
}
catch (Exception ex)
{
var errorMessage = ex.Message;
//TODO: handle errors;
}
return reportViewer;
}
public sealed class ReportServerCredentials : IReportServerCredentials
{
public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = null;
password = null;
authority = null;
return false;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null;
}
}
public ICredentials NetworkCredentials
{
get
{
string userName = ConfigurationManager.AppSettings["ReportUserName"];
if ((string.IsNullOrEmpty(userName)))
{
throw new Exception("Missing user name from web.config file");
}
string password = ConfigurationManager.AppSettings["ReportPassword"];
if ((string.IsNullOrEmpty(password)))
{
throw new Exception("Missing password from web.config file");
}
string domain = ConfigurationManager.AppSettings["DomainName"];
if ((string.IsNullOrEmpty(domain)))
{
throw new Exception("Missing domain from web.config file");
}
return new NetworkCredential(userName, password, domain);
}
}
}
[路线(“报告/月总结”)]
[ValidateAntiForgeryToken]
公共行动结果月总结(月报告参数模型)
{
if(ModelState.IsValid)
{
尝试
{
var actionType=model.actionType;
如果(actionType==“查看报告”)
{
返回ExportMonthlyReportToHtml(模型);
}
else if(actionType==“下载pdf报告”)
{
返回ExportMonthlyReportToPdf(型号);
}
}
捕获(例外情况除外)
{
//记录错误
}
}
返回null;
}
私有操作结果导出MonthlyReportToHTML(MonthlyReportParameters monthlyParams)
{
ReportViewer ReportViewer=BuildMonthlyReport(monthlyParams);
reportViewer.ServerReport.Refresh();
byte[]streamBytes=null;
字符串mimeType=“”;
字符串编码=”;
字符串filenameExtension=“”;
字符串[]streamids=null;
警告[]警告=null;
//以html格式查看报告的步骤
streamBytes=reportViewer.ServerReport.Render(“HTML4.0”,null,out mimeType,out encoding,out filenameExtension,out streamid,out warnings);
var htmlReport=File(streamBytes,“text/html”);
返回htmlReport;
}
专用静态ReportViewer BuildMonthlyReport(MonthlyReportParameters模型)
{
ReportViewer ReportViewer=新的Microsoft.Reporting.WebForms.ReportViewer();
尝试
{
var rptParameters=新列表
{
//建筑参数
};
reportViewer.ProcessingMode=ProcessingMode.Remote;
reportViewer.ServerReport.ReportPath=“/reportFolder/reportName”;
var reportServerUrl=ConfigurationManager.AppSettings[“reportServerUrl”];
如果(!string.IsNullOrEmpty(reportServerUrl))
{
reportViewer.ServerReport.ReportServerUrl=新Uri(ReportServerUrl);
}
reportViewer.ServerReport.ReportServerCredentials=新的ReportServerCredentials();
reportViewer.ServerReport.SetParameters(rptParameters);
}
捕获(例外情况除外)
{
var errorMessage=ex.Message;
//TODO:处理错误;
}
返回报表查看器;
}
公共密封类ReportServerCredentials:IReportServerCredentials
{
public bool GetFormsCredentials(out Cookie authCookie、out string用户名、out string密码、out string权限)
{
authCookie=null;
用户名=null;
密码=null;
权限=空;
返回false;
}
公共WindowsIdentity模拟用户
{
得到
{
返回null;
}
}
公共ICredentials网络凭据
{
得到
{
字符串用户名=ConfigurationManager.AppSettings[“ReportUserName”];
if((string.IsNullOrEmpty(userName)))
{
抛出新异常(“web.config文件中缺少用户名”);
}
字符串密码=ConfigurationManager.AppSettings[“ReportPassword”];
if((string.IsNullOrEmpty(密码)))
{
抛出新异常(“web.config文件中缺少密码”);
}
string domain=ConfigurationManager.AppSettings[“域名”];
if((string.IsNullOrEmpty(domain)))
{
抛出新异常(“web.config文件中缺少域”);
}
返回新的网络凭据(用户名、密码、域);
}
}
}
提前感谢,