How to use information from HttpContext in Custom Report Resolver
Environment
Product Version | 12.1 18.620 - 13.0.19.222 |
Product | Progress® Telerik® Reporting |
.NET Framework | .NET 4+ |
Important Note
From R1 2019 SP1 (13.0.19.222) the HttpContext may be accessed directly in the Custom Report Resolver.
Description
Starting with R1 2018 SP3 12.0.18.416 version, System.Web.HttpContext.Current is no longer available in the report rendering thread.
Removing HttpContext.Current in the report rendering thread was a necessary change. Its purpose was to allow the rendering engine to use a dedicated rendering thread queue with configurable count, which should significantly improve product's performance.
Solution
To access the current user context, you can use the Telerik.Reporting.Processing.UserIdentity.Current static property. It is also possible to use the new UserIdentity object in the expression context as a global object: =UserIdentity.
With the changes introduced in Telerik Reporting 12.1.18.620 it is possible to use the UserIdentity.Context property to store user objects (for example from the HttpContext). UserIdentity.Context can be filled up with the needed values in the ReportsController by overriding the GetUserIdentity() method. Here is a sample code :
// include in the ReportsController class
protected override UserIdentity GetUserIdentity()
{
var identity = base.GetUserIdentity();
identity.Context = new System.Collections.Concurrent.ConcurrentDictionary<string, object>();
identity.Context["UrlReferrer"] = System.Web.HttpContext.Current.Request.UrlReferrer;
// Any other available information can be stored in the identity.Context in the same way
return identity;
}
The UserIdentity.Current.Context["UrlRefferer"] should then be used (instead of HttpContext.Current.Request.UrlReferrer) to access the corresponding property/information. For example, you can access the UrlRefferer as :
// can be included in the Resolve() method of the Custom Report Resolver
Uri urlReferrer = (Uri)UserIdentity.Current.Context["UrlRefferer"];
Note
By default accessing a user’s session from a web API is not possible. Attempting to call HttpContext.Current.Session will always return Null. For more information and a workaround check Enabling session state in Web API blog post.
Specific solutions for MVC, Web Forms and .NET Core projects can be found in Accessing Session Using ASP.NET Web API Stackoverflow thread.