xtrareports-400660-feature-guide-to-devexpress-reports-bind-reports-to-data-json-data-provide-authentication-to-access-json-data-runtime-sample.md
This topic demonstrates two ways to provide a report’s JSON data source with authentication parameters at runtime.
Save Authentication Parameters to the Configuration File
A JsonDataSource object obtains settings from the configuration file. As a result, only the connection name is serialized to the report’s definition. The JsonDataSource ‘s JsonSource property is left unset.
Save Authentication Parameters to the Report’s Definition
A JsonDataSource object uses a UriJsonSource object assigned to the JsonSource property to specify connection settings. As a result, authentication parameters are serialized to the report’s definition.
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
Extend the application’s connection string with authentication parameters. Use this connection string to create a JsonDataSource object.
Specify a configuration string in your application’s configuration file.
Create a JsonDataSource object.
Assign the JsonDataSource object to your report.
Use the UriJsonSource object to specify authentication parameters. Assign this object to the JsonDataSource’s JsonSource property.
Create a UriJsonSource object and specify authentication parameters in it.
Create a JsonDataSource object and assign the UriJsonSource object to it.
Assign the JsonDataSource object to your report.
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();
}
}
}
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