reporting-docs/knowledge-base/core-how-to-pass-informatio...

3.9 KiB

title description type page_title slug position tags ticketid res_type
ASP.NET Core. How to use information from HttpContext in Custom Report Resolver ASP.NET Core. How to copy the needed information from HttpContext to UserIdentity.Context how-to ASP.NET Core. Pass HttpContext items to reporting engine core-how-to-pass-information-from-httpcontext-to-reporting-engine 1464320 kb

Environment

Product Progress® Telerik® Reporting
Product version 13.0.19.116
.Net Framework .NET Core 2.0+

Important Note

From R1 2019 SP1 (13.0.19.222) the HttpContext may be accessed directly in the Custom Report Resolver.

Description

If you try to use the approach from How to use information from HttpContext in Custom Report Resolver KB article in ASP.NET Core application, the Visual Studio will show a compile-time error with the following message

Error

System.Web.HttpContext is 'inaccessible due to its protection level' in ReportsController ASP.NET Core implementation

Solution

In .Net Core the HttpContext is accessible in different ways. For examples, see the following threads:

Here is an example of implementing one of the approaches:

  1. Add the following line in the Startup -> ConfigureServices method that will register the corresponding IHttpContextAccessor implementation in the default .NET Core Dependency Injection Container:
services.AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>();
  1. Declare a second parameter in the ReportsController constructor to inject the IHttpContextAccessor and use it to access the HttpContext. Here is a sample ReportsController implementation:
[Route("api/reports")]
public class ReportsController : ReportsControllerBase
{
    private IHttpContextAccessor httpContextAccessor;
	
    public ReportsController(IReportServiceConfiguration reportServiceConfiguration, IHttpContextAccessor httpContextAccessor)
        : base(reportServiceConfiguration)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    protected override UserIdentity GetUserIdentity()
    {
        var identity = base.GetUserIdentity();
        identity.Context = new System.Collections.Concurrent.ConcurrentDictionary<string, object>();

        // *The following code line is suitable for .NET and not for .NET Core. When used in .NET Core
        // *compile time there will be an error
        // *"'HttpContext' is inaccessible due to its protection level"
        //identity.Context["UrlReferrer"] = System.Web.HttpContext.Current.Request.UrlReferrer;
		
        // *The following code line is suitable for .NET Core 
        identity.Context["UrlReferrer"] = httpContextAccessor.HttpContext.Request.Headers["Referer"].ToString();

        // 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 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"];