Skip to main content

Provide Authentication to Access JSON Data (Runtime Sample)

  • 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.

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.

    The following connection string illustrates how to specify basic authentication parameters, token-based authentication parameters and query parameters.

    <connectionStrings>
      <add name="JsonConnection" connectionString="Uri=http://northwind.servicestack.net/customers.json;           UserName=user;Password=pwd;           header:MyAuthHeader1=secretToken1;header:MyAuthHeader2=secretToken2;           query:id=123456;query:name=MyName" providerName="JsonSourceProvider" />
    </connectionStrings>
    
  2. Create a JsonDataSource object.

    public static JsonDataSource CreateReportDataSourceFromConnectionString() {
        JsonDataSource jsonDataSource = new DevExpress.DataAccess.Json.JsonDataSource() {
            // The application's configuration file should include the "JsonConnection" connection string
            ConnectionName = "JsonConnection"
        };
        return jsonDataSource;
    }
    
  3. Assign the JsonDataSource object to your report.

    private void CreateReportDataSourceFromConnectionStringButton_Click(object sender, EventArgs e) {
        // XtraReport1 does not have assigned data sources
        var report = new XtraReport1(); 
    
        // Create JsonDataSource from the specified connection string
        var jsonDataSource = CreateReportDataSourceFromConnectionString();
        // Retrieve data to populate the Report Designer's Field List
        jsonDataSource.Fill();
    
        report.DataSource = jsonDataSource;
        report.DataMember = "Customers";
    
        new DevExpress.XtraReports.UI.ReportDesignTool(report).ShowDesigner();
    }
    

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.

    public static JsonDataSource CreateReportDataSourceWithAuthenticationInCode() {
    // ...
        // Create a new UriJsonSource object and configure authentication data in it
        var jsonSource = new DevExpress.DataAccess.Json.UriJsonSource();
        jsonSource.Uri = new Uri(@"http://northwind.servicestack.net/customers.json");
    
    
        jsonSource.AuthenticationInfo.Username = "user";
        jsonSource.AuthenticationInfo.Password = "pwd";
    
        jsonSource.HeaderParameters.Add(new HeaderParameter("MyAuthHeader1", "secretToken1"));
        jsonSource.HeaderParameters.Add(new HeaderParameter("MyAuthHeader2", "secretToken2"));
    
        jsonSource.QueryParameters.Add(new QueryParameter("id", "123456"));
        jsonSource.QueryParameters.Add(new QueryParameter("name", "MyName"));
        // ...
    }
    
  2. Create a JsonDataSource object and assign the UriJsonSource object to it.

    public static JsonDataSource CreateReportDataSourceWithAuthenticationInCode() {
    // ...
        // Create a JsonDataSource object and assign the UriJsonSource object to it
        var jsonDataSource = new DevExpress.DataAccess.Json.JsonDataSource() {
            JsonSource = jsonSource
        };
    
        return jsonDataSource;
        // ...
    }
    
  3. Assign the JsonDataSource object to your report.

    private void CreateReportDataSourceWithAuthenticationInCodeButton_Click(object sender, EventArgs e) {
        // XtraReport1 does not have assigned data sources
        var report = new XtraReport1();
    
        // Create JsonDataSource in code
        JsonDataSource jsonDataSource = CreateReportDataSourceWithAuthenticationInCode();
        // Retrieve data to populate the Report Designer's Field List
        jsonDataSource.Fill();
    
        report.DataSource = jsonDataSource;
        report.DataMember = "Customers";
    
        new DevExpress.XtraReports.UI.ReportDesignTool(report).ShowDesigner();
    }
    

Migrate from v18.2

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

v18.2 v19.1+
Implement a custom UriJsonSource class to provide authentication parameters The 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.

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();
        }
    }
}