Configure global OAuth2.0 or JWT authentication for DataManager

Hello,

I'm searching a way to configure authentication for all DataManager used in my application (using Syncfusion.Blazor v21.1.37).

For instance, we could have a Syncfusion method called at my application startup (in Program.cs) to configure the way that all DataManagers can get a JWT token before send the GET request to an API to get data.

Is there a way to do it ? Because it could be annoying to configure Headers for all DataManagers in the application (as described here : https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.DataManager.html#Syncfusion_Blazor_DataManager_Headers).


7 Replies 1 reply marked as answer

PS Prathap Senthil Syncfusion Team August 29, 2023 12:59 PM UTC

Hi Julien,

Based on your requirements, we have already discussed this in our user guide documentation. Kindly refer to the documentation below for your reference.

Reference Link: 
https://blazor.syncfusion.com/documentation/datagrid/data-binding#configuring-httpclient


Regards,
Prathap S



JG Julien Gambini August 30, 2023 08:22 AM UTC

Hi Prathap,

I tried to configure an HttpClient at my application startup like this :

Image_7526_1693383567690

But the SfDataManager can't get data from the API. But when I inject an HttpClient in one of my Blazor component with the same authentication method and pass it to the SfDataManager HttpClientInstance directly, it is working properly.

What am I doing wrong here ?



PS Prathap Senthil Syncfusion Team September 5, 2023 07:10 AM UTC

Before proceeding with the reporting problem, we require some additional clarification from your end. Please share the below details to proceed further at our end.

  1. Share with us a grid code snippet with the model class.
  2. Please provide us with a simple, reproducible sample of the issue.
  3. Share with us the NuGet version you have used.
  4. If possible, kindly share with us your attempts to reproduce the reported issue on the attached sample.

Above-requested details will be very helpful in validating the reported query at our end and providing a solution as early as possible.

Reference:https://blazor.syncfusion.com/documentation/datagrid/data-binding#authorization-and-authentication


Attachment: DataGridSample_9eced973.zip


JG Julien Gambini September 5, 2023 12:35 PM UTC

I always have to use HttpClientInstance="httpClient" in all SfDataManager components everywhere ?

I cannot define a HttpClient implicitely to avoid to set the  HttpClientInstance property all over my application ?



PS Prathap Senthil Syncfusion Team September 15, 2023 11:40 AM UTC

To define the HttpClient globally and avoid setting the HttpClientInstance property all over your application, you can create a custom component that encapsulates the SfDataManager and sets the HttpClient at a global level. Create a custom Blazor component that wraps the SfDataManager and configures the HttpClient in its OnInitializedAsync method.

@inherits ComponentBase

@inject IHttpClientFactory HttpClientFactory

@using Syncfusion.Blazor.Data

@using Syncfusion.Blazor

<SfDataManager Url="api/Default" Adaptor="Adaptors.WebApiAdaptor" HttpClientInstance="httpClient" CrossDomain="true"></SfDataManager>

 

@code {

    private HttpClient httpClient;

 

   

     protected override async Task OnInitializedAsync()

    {

        httpClient = HttpClientFactory.CreateClient("WebAPI");

        await base.OnInitializedAsync();

    }

 

}

 


Register this custom component in your application:

In your _Imports.razor file, add an import statement for the namespace where you define the custom component (e.g., MyApp.Components):"


@using BlazorApp5.Client.MyComponent



Use the custom component wherever you need the SfDataManager that is configured globally with the HttpClient.

    <MyCustomSfDataManager></MyCustomSfDataManager>

 



With this approach, you only need to define the HttpClient configuration in your Program.cs file once and then use the MyCustomSfDataManager component throughout your application without specifying HttpClientInstance. This encapsulates the HttpClient setup and makes your code cleaner and more maintainable. For your reference, we have attached a modified sample.


Attachment: DataGridWebApi_f81a1032.zip

Marked as answer

JG Julien Gambini September 18, 2023 06:03 PM UTC

This is perfect, thanks!



NP Naveen Palanivel Syncfusion Team September 20, 2023 01:43 AM UTC

Hi Julien,


Welcome


Please get back to us if you need further assistance.


Regards,

Naveen Palanivel.


Loader.
Up arrow icon