How to pass the authorization header from report service requests in Syncfusion ASP.NET reportviewer
You can use the Ajax-Before-Load event to pass the authorization header with report server requests using Syncfusion ASP.NET 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 the client-side and pass as an argument for the Ajax-before-load event as shown in the following code snippet.
Default.aspx
<form id="form1" runat="server">
<div style="height: 650px;width: 950px;min-height:404px;">
<ej:ReportViewer ID="ReportViewer1" runat="server" ReportPath="~/App_Data/GroupingAgg.rdl" ajaxBeforeLoad="ajaxBeforeLoad" ProcessingMode="Remote"/>
</div>
</form>
<script type="text/javascript">
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)
{
}
}