Back to Devexpress

Provide Authentication to Access JSON Data (Runtime Sample)

xtrareports-400660-feature-guide-to-devexpress-reports-bind-reports-to-data-json-data-provide-authentication-to-access-json-data-runtime-sample.md

latest7.2 KB
Original Source

Provide Authentication to Access JSON Data (Runtime Sample)

  • Feb 18, 2026
  • 6 minutes to read

This topic demonstrates two ways to provide a report’s JSON data source with authentication parameters at runtime.

Note

These approaches are available in v19.1+. Refer to the Migrate from v18.2 section for information on how to migrate from the approach v18.2.

Tip

Online Example : How to Provide Authentication to Access JSON Data

Save Authentication Parameters to the Configuration File

Extend the application’s connection string with authentication parameters. Use this connection string to create a JsonDataSource object.

  1. Specify a configuration string in your application’s configuration file.

  2. Create a JsonDataSource object.

  3. Assign the JsonDataSource object to your report.

Save Authentication Parameters to the Report’s Definition

Use the UriJsonSource object to specify authentication parameters. Assign this object to the JsonDataSource’s JsonSource property.

  1. Create a UriJsonSource object and specify authentication parameters in it.

  2. Create a JsonDataSource object and assign the UriJsonSource object to it.

  3. Assign the JsonDataSource object to your report.

Migrate from v18.2

The following table illustrates the different approaches you use to provide JSON authentication in v18.2 and v19.1+:

v18.2v19.1+
Implement a custom UriJsonSource class to provide authentication parametersThe UriJsonSource class includes the AuthenticationInfo , HeaderParameters and QueryParameters properties.
Implement a custom wizard page based on the Specify JSON Data Location page to provide authentication parameter editors. Register this page.The Data Source Wizard includes two extra pages: Configure Web Service Endpoint Request and Save the Connection String.

To migrate reports from v18.2 to v19.1+, call the method demonstrated in the code below. This method converts a specified report definition as if you use the Save Authentication Parameters to the Configuration File approach.

csharp
public static class JsonDatasourceAuthorization_Example {
    public static string ConvertReportWithMyUriJsonSourceTo191(string repxContent, out List<string> connectionString) {
        var report = new XtraReport();
        using(var ms = new MemoryStream(Encoding.UTF8.GetBytes(repxContent))) {
            report.LoadLayoutFromXml(ms);
        }

        connectionString = new List<string>();

        int i = 0;
        foreach(var component in report.ComponentStorage) {
            var jsonDS = (component as DevExpress.DataAccess.Json.JsonDataSource);
            var jsonSource = (jsonDS?.JsonSource as MyUriJsonSource);
            if(jsonSource != null) {
                i++;
                jsonDS.ConnectionName = string.Format("newJsonConnection_{0}{1}", report.Name, i.ToString());

                var builder = new DbConnectionStringBuilder();
                builder.Add("Uri", jsonSource.Uri.OriginalString);
                builder.Add("UserName", jsonSource.UserName);
                builder.Add("Password", jsonSource.Password);

                connectionString.Add(string.Format("<add name=\"{0}\" connectionString=\"{1}\" providerName=\"JsonSourceProvider\" />",
                    jsonDS.ConnectionName, builder.ConnectionString));

                jsonDS.JsonSource = null;
            }
        }
        using(var ms = new MemoryStream()) {
            report.SaveLayoutToXml(ms);
            ms.Position = 0;
            StreamReader reader = new StreamReader(ms);
            return reader.ReadToEnd();
        }
    }
}
vb
Public NotInheritable Class JsonDatasourceAuthorization_Example

    Private Sub New()
    End Sub

    Public Shared Function ConvertReportWithMyUriJsonSourceTo191(ByVal repxContent As String,  ByRef connectionString As List(Of String)) As String
        Dim report = New XtraReport()
        Using ms = New MemoryStream(Encoding.UTF8.GetBytes(repxContent))
            report.LoadLayoutFromXml(ms)
        End Using

        connectionString = New List(Of String)()

        Dim i As Integer = 0
        For Each component In report.ComponentStorage
            Dim jsonDS = (TryCast(component, DevExpress.DataAccess.Json.JsonDataSource))
            Dim jsonSource = (TryCast(jsonDS?.JsonSource, MyUriJsonSource))
            If jsonSource IsNot Nothing Then
                i += 1
                jsonDS.ConnectionName = String.Format("newJsonConnection_{0}{1}", report.Name, i.ToString())

                Dim builder = New DbConnectionStringBuilder()
                builder.Add("Uri", jsonSource.Uri.OriginalString)
                builder.Add("UserName", jsonSource.UserName)
                builder.Add("Password", jsonSource.Password)

                connectionString.Add(String.Format("<add name=""{0}"" connectionString=""{1}"" providerName=""JsonSourceProvider"" />", jsonDS.ConnectionName, builder.ConnectionString))

                jsonDS.JsonSource = Nothing
            End If
        Next component
        Using ms = New MemoryStream()
            report.SaveLayoutToXml(ms)
            ms.Position = 0
            Dim reader As New StreamReader(ms)
            Return reader.ReadToEnd()
        End Using
    End Function
End Class