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).
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
Hi Prathap,
I tried to configure an HttpClient at my application startup like this :
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 ?
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.
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
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 ?
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):"
|
|
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.
This is perfect, thanks!
Hi Julien,
Welcome
Please get back to us if you need further assistance.
Regards,
Naveen Palanivel.