0

I created a Telerik report from a separate project. I have report viewer from my ASP.NET MVC project to load the report.

Here is the code:

private ViewResult ReportViewer(Report report)
{
    report.ExternalStyleSheets.Clear();

    var serverUrl = UrlExtension.BaseUrl();
    var relativeUrl = Url.Action("ReportStyleSheet", "Theme");

    var url = string.Format("{0}{1}", serverUrl, relativeUrl);

    var styleSheet = new ExternalStyleSheet(url);

    report.ExternalStyleSheets.Add(styleSheet);

    return View("ReportViewer", report);
}

The action ReportStyleSheet from the Theme controller requires authentication

After successful login and try to view the report, The report can't access the /Theme/ReportStyleSheet. But when I run the Url from the browser its authenticated. How can I resolve this issue?

Its using Form Authentication.

When trying to pass MemoryStream to ExternalStyleSheet constructor, an error occurred that says An error has occurred while processing Report 'MyReport': Buffer cannot be null. Parameter name: buffer . I am sure that the stream was passed. Here is the screenshot: enter image description here

h3n
  • 5,142
  • 9
  • 46
  • 76

2 Answers2

0

Make sure you have configured Authentication in web.config file to avoid application ambiguity. Types of Authentications will be found here. Just note that you have not another enabled authentication.

Community
  • 1
  • 1
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
0

Please take a look at ExternalStyleSheet documentation. It is not well written but to me it seems that the stylesheet is loaded immediately after you call

var styleSheet = new ExternalStyleSheet(url);

Therefore it is called by web server (app pool user that your web pages runs under). This user is not authenticated and that is the cause of your problems.

I would suggest you take the stylesheet to a location that does not require authentication or that you use another overload of ExternalStyleSheet constructor that does not load from Uri.

Based on your screenshot and the following conversation in comments I have created similar class

public class MyReport : Telerik.Reporting.Report
{
    public MyReport(string stylesheet)
    {
        this.ExternalStyleSheets.Clear();
        using (System.IO.Stream s = new System.IO.MemoryStream(File.ReadAllBytes(stylesheet)))
            this.ExternalStyleSheets.Add(new ExternalStyleSheet(s));
    }
}

It works. Maybe you should use System.Text.Encoding.UTF8.GetBytes(styles) or load the stylesheet from file. Maybe it has something to do with your stylesheet encoding. Mine was utf-8.

pepo
  • 8,644
  • 2
  • 27
  • 42
  • It needs to be authenticated because the styles in the xml stylesheet are dynamic based on the settings of the users organization. If only there's a constructor that accepts xml string, that will be easier. – h3n Feb 21 '14 at 00:46
  • @h3n OK, so when you are in ReportViewer action do you know the identity of organization to which you want to serve the stylesheet? If so then I would suggest to use the construstor with stream parameter and load the correct stylesheet as stream. – pepo Feb 21 '14 at 06:29
  • I added this to the report constructor and an error occurred that says: An error has occurred while processing Report 'MyReport': Buffer cannot be null. Parameter name: buffer this.ExternalStyleSheets.Clear(); using (System.IO.Stream s = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(styles))) this.ExternalStyleSheets.Add(new ExternalStyleSheet(s)); – h3n Feb 24 '14 at 03:42
  • @h3n Are you sure that there is a stylesheet in your memory stream? I tried to export stylesheet from invoice example of telerik reporting package using [this](http://www.telerik.com/help/reporting/style-exportingand-reusing-style-sheets.html) documentation. It seems that you can load stylesheet from absolute path. So I prepared a `new Report()` and loaded stylesheet using `var styleSheet = new ExternalStyleSheet(new Uri(@"c:\invoice.xml"));`. I have also tried to load the file using code from your comment. It worked too. The stylesheet was included in the Report class. – pepo Feb 24 '14 at 09:18
  • I updated the description and included the screenshot. You will see that the stream was passed to the stylesheet. – h3n Feb 24 '14 at 10:12