Use ISettingsStorage instead of appsettings in the ReportViewer to acces SharedDataSource

1 Answer 74 Views
Report Viewer - Blazor
Víctor
Top achievements
Rank 1
Iron
Iron
Iron
Víctor asked on 08 Feb 2024, 03:25 PM

Hello Telerik,

I'm currently developing an application using Blazor Server along with the balzor Report Designer/Viewer. 

We are a multitenant application that has multiples dbs per tenant. I'm facing the challenge of ensuring that connection strings are safe and flexible. I need each tenant to be able to create and print reports without interacting with the connection string at any time. So want to use Shared Data Sources.

Storing them in the report with the embedded in the report definition option is not safe nor flexible enough and the shared connection with alias and read it from appsettings file doesn't provide the level of security I require security nor flexibility. 

I have a custom implementation of the ISettingsStorage. That works well in the designer, it allows me to provide a shared connection string from custom sources (stored and encrypted in a db). But I can't seem to make the ISettingsStorage work with the ReportViewer, it seems like it allways tries to find the shared data sources from appsetings, wich is not desired. I'm using blazor designer and viewer in the same server.

TLDR: I need to use ISettingsStorage instead of appsettings in the ReportViewer with shared SQL Server data source.

Can you help me please?

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 13 Feb 2024, 12:26 PM

Hello Victor,

Thank you for the provided information!

When it comes to the report viewer, to use a configuration other than what is loaded in the appsettings.json file of your application, you may set a custom-created IConfiguration instance to the ReportServiceConfiguration.ReportingEngineConfiguration property, for example:

// Configure dependencies for ReportsController.
builder.Services.TryAddSingleton<IReportServiceConfiguration>(sp =>
    new ReportServiceConfiguration
    {
        ReportingEngineConfiguration = ResolveSpecificReportingConfiguration(sp.GetService<IWebHostEnvironment>()),
        HostAppId = "ReportingNet6",
        Storage = new FileStorage(),
        ReportSourceResolver = new TypeReportSourceResolver()
                                    .AddFallbackResolver(
                                        new UriReportSourceResolver(reportsPath))
    });
static IConfiguration ResolveSpecificReportingConfiguration(IWebHostEnvironment environment)
{
    var builder = new ConfigurationBuilder();
    IConfiguration built = null;

    var jsonConnection = new
    {
        ConnectionStrings = new
        {
            MyConnectionAlias = new {
                connectionString = Environment.GetEnvironmentVariable("CONN_STRING"),
                providerName = "System.Data.SqlClient"
            },
            MyConnectionAliasII = new
            {
                connectionString = Environment.GetEnvironmentVariable("CONN_STRING_II"),
                providerName = "System.Data.SqlClient"
            }
        }
    };

    var json = JsonSerializer.Serialize(jsonConnection);

    using (MemoryStream ms = new MemoryStream())
    {
        using (StreamWriter sw = new StreamWriter(ms))
        {
            sw.Write(json);
            sw.Flush();
            ms.Position = 0;

            builder.AddJsonStream(ms);
            built = builder.Build();
        }
    }

    return built;
}

Please give this approach a try and let me know how it goes.

Regards,
Dimitar
Progress Telerik

Stay tuned by visiting our roadmap and feedback portal pages, enjoy a smooth take-off with our Getting Started resources, or visit the free self-paced technical training at https://learn.telerik.com/.
Víctor
Top achievements
Rank 1
Iron
Iron
Iron
commented on 13 Feb 2024, 04:06 PM

Hy Dimitar,

Thanks, that's very neat. I'll give it a go!

Tags
Report Viewer - Blazor
Asked by
Víctor
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or