How to pass authorization header from report service requests in Syncfusion JavaScript reportviewer?
You can use the Ajax-Before-Load event to pass the authorization header with report server requests using Syncfusion JavaScript ReportViewer. The following steps guide you to send the custom headers from client to the server-side using the ‘Ajax-before-load’ event.
1. Create a simple ReportViewer application with the help of getting started documentation.
2. Provide the custom headers in client-side and pass as an argument for the Ajax-before-load event as shown in the following code snippet.
Default.html
<script type="text/javascript">
$(function () {
$("#container").ejReportViewer(
{
reportServiceUrl: "/api/ReportApi",
reportPath: '~/App_Data/GroupingAgg.rdl',
processingMode: ej.ReportViewer.ProcessingMode.Remote,
ajaxBeforeLoad: "ajaxBeforeLoad"
});
});
function ajaxBeforeLoad(event) {
event.headers.push({ Key: 'Authorization', Value: "BearerMyToken" });
};
</script>
3. Get the client-side custom header in the controller (ReportAPIController) from Authorization key using HttpContext in PostReportAction as shown in the following code snippet.
ReportAPIController.cs
public class ReportApiController : ApiController,IReportController
{
public string HeaderContent = null;
//Post action for processing the rdl/rdlc report
public object PostReportAction(Dictionary<string, object> jsonResult)
{
if (jsonResult != null)
{
HeaderContent = HttpContext.Current.Request.Headers["Authorization"];
}
return ReportHelper.ProcessReport(jsonResult, this);
}
[System.Web.Http.ActionName("GetResource")]
[AcceptVerbs("GET")]
public object GetResource(string key, string resourcetype, bool isPrint)
{
return ReportHelper.GetResource(key, resourcetype, isPrint);
}
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
}
public void OnReportLoaded(ReportViewerOptions reportOption)
{
}
}
Please download the sample for authorization header from here