1

I am creating one web app (mvc 4) to authorize customers (using membership provider) to view the reports(SSRS 2008) for which they are registered but they don't have any kind of access to our report server.

Based on the link How do I render a remote ReportViewer aspx page in MVC4?, I have implemented Elsimer's latest answer and it works well in downloading as a pdf file.

But when I try to render as html using the same code mentioned in the above link it is asking for the windows credentials to access the report server.

So I am giving a general credential which has all access to all the reports in the reportserver through the code. but it is still asking for the credentials for the report server when they try to view as html in the client side browser. Report is getting rendered but the images and graphs are not rendering without credentials.

Please advise, I have tried many things to solve this. but no luck.

My controller and credential class code as follows:

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

}

Thanks in advance,

Community
  • 1
  • 1

0 Answers0